示例#1
0
    protected void OnCollide(Collider collider, Vector3 collisionPoint, bool hasPoint = false)
    {
        Hitbox hitbox = collider.GetComponent <Hitbox>();

        if (hitbox != null && !hitboxes.Exists(entry => entry.hitbox == hitbox) && hitbox.CanBeHit(this) && !IsTransformExcluded(collider.transform))
        {
            Vector3 direction = tr.position - prevPosition;

            // If direction isn't equal to zero, then we haven't been moving! So fall back to treating this like a non-projectilve damage dealer.
            // Also if this raycast fails, then use the fallback as well.
            Ray        ray = new Ray(prevPosition, direction);
            RaycastHit hit;
            if (isProjectile && direction != Vector3.zero && collider.Raycast(ray, out hit, (tr.position - prevPosition).magnitude * 2.1f))
            {
                hitboxes.Add(new HitboxEntry(hitbox, hit.point, hit.distance));
            }
            else
            {
                //Use the backup point if we've got it. Score it's distance based on our previous position.
                if (hasPoint)
                {
                    hitboxes.Add(new HitboxEntry(hitbox, collisionPoint, (collisionPoint - prevPosition).magnitude));
                }
                else
                {
                    //If we don't have a backup point, then do a raycast towords the collider's center that will almost certainly get us a point.
                    // Raycast from our current position to the center of the collider bounds.
                    Vector3 boundsVector = collider.bounds.center - tr.position;
                    ray = new Ray(tr.position, boundsVector);
                    if (collider.Raycast(ray, out hit, boundsVector.magnitude + 0.05f))
                    {
                        hitboxes.Add(new HitboxEntry(hitbox, hit.point, (hit.point - prevPosition).magnitude));
                    }
                    else
                    {
                        // If that failed, then either there's some wonky concave stuff going on or we're inside of the other collider!
                        // So let's just assume that we're inside the other collider because the other possible edge cases are so obscure that I'm not going to waste extra raycasts/math on them.
                        hitboxes.Add(new HitboxEntry(hitbox, tr.position, (tr.position - prevPosition).magnitude));
                    }
                }
            }

            // Make sure we start the post fixed update coroutine if it's not already running.
            if (coroutineResolveDamage == null)
            {
                coroutineResolveDamage = StartCoroutine(ResolveDamage());
            }
        }
    }