private void Update() { //maybe die if (StayTime > 0) { Elapsed += Time.deltaTime; if (Elapsed >= StayTime) { //Debug.Log($"Destroying {name} at {Elapsed:F2}s/{StayTime:F2}"); Destroy(this.gameObject); } } if (transform.position.magnitude > MaxDist) { //Debug.Log($"Destroying {name} because it's really far away"); Destroy(this.gameObject); } //raycast bool hasRigidbody = Rigidbody != null; Vector3 forward = hasRigidbody ? Rigidbody.velocity.normalized : transform.forward; float maxDistance = hasRigidbody ? Rigidbody.velocity.magnitude / 30f : DefaultProbeDist; //var hits = Physics.RaycastAll(transform.position, forward, maxDistance, RaycastLayerMask, QueryTriggerInteraction.Collide); //find closest hit //var (otherController, hitPoint, hitLocation, hitMaterial) = GetClosestHit(hits); var hit = WorldUtils.RaycastAttackHit(transform.position, forward, maxDistance, true, true, null); if (hit.Controller != null && hit.Controller != HitInfo.Originator) { //Debug.Log("Bullet hit " + otherController.name + " via raycast!"); float damageMultiplier; bool allDamageIsPierce; if (hit.Hitbox != null) { //Debug.Log((hit.Hitbox as MonoBehaviour)?.name); damageMultiplier = hit.Hitbox.DamageMultiplier; allDamageIsPierce = hit.Hitbox.AllDamageIsPierce; } else { damageMultiplier = 1; allDamageIsPierce = false; } HandleCollision(hit.Controller, hit.HitLocation, hit.HitMaterial, hit.HitPoint, damageMultiplier, allDamageIsPierce); } }
private void Update() { //TODO pause handling? //maybe die if (StayTime > 0) { Elapsed += Time.deltaTime; if (Elapsed >= StayTime && !DeferredHitBegan) { //Debug.Log($"Destroying {name} at {Elapsed:F2}s/{StayTime:F2}"); Destroy(this.gameObject); } } //distance travel check if (MaxDistToTravel > 0 && !DeferredHitBegan) { float distance = (Origin - transform.position).magnitude; if (distance > MaxDistToTravel) { Destroy(this.gameObject); } } if (MaxDistFromOrigin >= 0 && transform.position.magnitude > MaxDistFromOrigin && !DeferredHitBegan) { //Debug.Log($"Destroying {name} because it's really far away"); Destroy(this.gameObject); } if (DeferredHitBegan && TimeToDeferredHit > 0) { TimeToDeferredHit -= Time.deltaTime; if (TimeToDeferredHit <= 0) { //handle deferred hit DeferredHitAction(); } } if (EnableRaycasting && !DeferredHitBegan) { //raycast bool hasRigidbody = Rigidbody != null; Vector3 forward = hasRigidbody ? Rigidbody.velocity.normalized : transform.forward; float maxDistance = OverrideProbeDist > 0 ? OverrideProbeDist : (hasRigidbody ? Rigidbody.velocity.magnitude / 30f : DefaultProbeDist); //var hits = Physics.RaycastAll(transform.position, forward, maxDistance, RaycastLayerMask, QueryTriggerInteraction.Collide); //find closest hit //var (otherController, hitPoint, hitLocation, hitMaterial) = GetClosestHit(hits); var hit = WorldUtils.RaycastAttackHit(transform.position, forward, maxDistance, true, true, null); if (hit.Controller != null && hit.Controller != HitInfo.Originator) { //Debug.Log("Bullet hit " + hit.Controller.name + " via raycast!"); float damageMultiplier; bool allDamageIsPierce; if (hit.Hitbox != null) { //Debug.Log((hit.Hitbox as MonoBehaviour)?.name); damageMultiplier = hit.Hitbox.DamageMultiplier; allDamageIsPierce = hit.Hitbox.AllDamageIsPierce; } else { damageMultiplier = 1; allDamageIsPierce = false; } if (EnableDeferredHits) { DeferredHitBegan = true; DeferredHitAction = () => HandleHit(hit.Controller, hit.Hitbox, hit.HitLocation, hit.HitMaterial, hit.HitPoint, damageMultiplier, allDamageIsPierce); TimeToDeferredHit = (transform.position - hit.HitPoint).magnitude / Rigidbody.velocity.magnitude; //Debug.Log($"bullet: {transform.position} | target: {hit.HitPoint} | distance: {(transform.position - hit.HitPoint).magnitude} | velocity: {Rigidbody.velocity.magnitude}"); } else { HandleHit(hit.Controller, hit.Hitbox, hit.HitLocation, hit.HitMaterial, hit.HitPoint, damageMultiplier, allDamageIsPierce); } } else if (hit.Controller == null && hit.HitCollider != null) { var hitMaterial = hit.HitMaterial; var colliderHitMaterial = hit.HitCollider.gameObject.GetComponent <ColliderHitMaterial>(); if (colliderHitMaterial != null) { hitMaterial = colliderHitMaterial.Material; } if (EnableDeferredHits) { DeferredHitBegan = true; DeferredHitAction = () => HandleHit(null, null, hit.HitLocation, hitMaterial, hit.HitPoint, 1, false); TimeToDeferredHit = (transform.position - hit.HitPoint).magnitude / Rigidbody.velocity.magnitude; //Debug.Log($"bullet: {transform.position} | target: {hit.HitPoint} | distance: {(transform.position - hit.HitPoint).magnitude} | velocity: {Rigidbody.velocity.magnitude}"); } else { HandleHit(null, null, hit.HitLocation, hitMaterial, hit.HitPoint, 1, false); } } } }