private void OnTriggerStay(Collider other)
        {
            //Only allow damage to be applied this way if the collider is a multi-hit collider
            if (!IsMultiHit || other.gameObject == Owner || !CheckHitTime())
            {
                return;
            }

            ColliderBehaviour otherCollider   = null;
            GameObject        otherGameObject = null;

            if (other.attachedRigidbody)
            {
                otherGameObject = other.attachedRigidbody.gameObject;
                otherCollider   = otherGameObject.GetComponent <ColliderBehaviour>();
            }
            else
            {
                otherGameObject = other.gameObject;
            }

            if (otherCollider && IgnoreColliders)
            {
                return;
            }

            Vector3 collisionDirection = (otherGameObject.transform.position - transform.position).normalized;

            OnHit?.Invoke(otherGameObject, otherCollider, collisionDirection);

            if (DestroyOnHit)
            {
                Destroy(gameObject);
            }
        }
示例#2
0
        //Called when ability is used
        protected override void Activate(params object[] args)
        {
            base.Activate(args);
            //Set the amount of frames the projectile will register a hit
            ProjectileCollider.HitFrames = 1;
            //Create a new collider that will handle reversing velocity
            _reboundCollider = ActiveProjectiles[0].AddComponent <ColliderBehaviour>();

            //Initialize rebound collider
            _reboundCollider.Init(false, 0, owner, false, true);
            //Redirect projectile on hit
            _reboundCollider.OnHit += TryRedirectProjectile;
        }
        /// <summary>
        /// Checks if the object it collided with is an enemy projectile.
        /// If so, reverses velocity
        /// </summary>
        /// <param name="gameObject"></param>
        public void TryReflectProjectile(GameObject gameObject)
        {
            //Get collider and rigidbody to check the owner and add the force
            ColliderBehaviour otherHitCollider = gameObject.GetComponentInParent <ColliderBehaviour>();
            Rigidbody         otherRigidbody   = gameObject.GetComponentInParent <Rigidbody>();

            //If the object collided with is an enemy projectile...
            if (otherHitCollider && otherRigidbody && !otherHitCollider.CompareTag("Player") && !otherHitCollider.CompareTag("Entity"))
            {
                //...reset the active time and reverse its velocity
                otherHitCollider.ColliderOwner = owner;
                otherHitCollider.ResetActiveTime();
                otherRigidbody.AddForce(-otherRigidbody.velocity * 2, ForceMode.Impulse);
            }
        }
        private void OnTriggerEnter(Collider other)
        {
            //If the object has already been hit or if the collider is multihit return
            if (Collisions.Contains(other.gameObject) || IsMultiHit || other.gameObject == Owner)
            {
                return;
            }

            ColliderBehaviour otherCollider   = null;
            GameObject        otherGameObject = null;

            if (other.attachedRigidbody)
            {
                otherGameObject = other.attachedRigidbody.gameObject;
                otherCollider   = otherGameObject.GetComponent <ColliderBehaviour>();
            }
            else
            {
                otherGameObject = other.gameObject;
            }

            int mask = LayerMask.GetMask(_layersToIgnore.ToArray());

            if (otherCollider && IgnoreColliders /* || mask != (mask | 1 << otherGameObject.layer)*/)
            {
                return;
            }

            //Add the game object to the list of collisions so it is not collided with again
            Collisions.Add(other.gameObject);

            Vector3 collisionDirection = (otherGameObject.transform.position - transform.position).normalized;

            OnHit?.Invoke(otherGameObject, otherCollider, collisionDirection);

            if (DestroyOnHit)
            {
                Destroy(gameObject);
            }
        }
        private void OnCollisionEnter(Collision collision)
        {
            //If the object has already been hit or if the collider is multihit return
            if (Collisions.Contains(collision.gameObject) || IsMultiHit || collision.gameObject == Owner)
            {
                return;
            }

            ColliderBehaviour otherCollider   = null;
            GameObject        otherGameObject = null;

            if (collision.collider.attachedRigidbody)
            {
                otherGameObject = collision.collider.attachedRigidbody.gameObject;
                otherCollider   = otherGameObject.GetComponent <ColliderBehaviour>();
            }
            else
            {
                otherGameObject = collision.gameObject;
            }

            if (otherCollider && IgnoreColliders)
            {
                return;
            }

            //Add the game object to the list of collisions so it is not collided with again
            Collisions.Add(collision.gameObject);

            Vector3 collisionDirection = (otherGameObject.transform.position - transform.position).normalized;

            OnHit?.Invoke(collision.gameObject, collision, collisionDirection);

            if (DestroyOnHit)
            {
                Destroy(gameObject);
            }
        }
        private void OnTriggerEnter(Collider other)
        {
            //If the object has already been hit or if the collider is multihit return
            if (Collisions.Contains(other.gameObject) || IsMultiHit)
            {
                return;
            }

            ColliderBehaviour otherCollider = null;

            if (other.attachedRigidbody)
            {
                if (other.attachedRigidbody.gameObject != Owner)
                {
                    otherCollider = other.attachedRigidbody.gameObject.GetComponentInChildren <ColliderBehaviour>();
                }
                else
                {
                    return;
                }
            }

            if (other.CompareTag("ParryBox"))
            {
                return;
            }

            if (otherCollider && !other.CompareTag("Player") && !other.CompareTag("Entity"))
            {
                if (IgnoreColliders || otherCollider.IgnoreColliders || otherCollider.ColliderOwner == Owner)
                {
                    return;
                }
                else if (otherCollider is HitColliderBehaviour)
                {
                    if (((HitColliderBehaviour)otherCollider).Priority >= Priority && otherCollider.ColliderOwner != ColliderOwner)
                    {
                        Destroy(gameObject);
                        return;
                    }

                    return;
                }
            }

            float newHitAngle = _hitAngle;

            //Calculates new angle if this object should change trajectory based on direction of hit
            if (_adjustAngleBasedOnCollision)
            {
                //Find a vector that point from the collider to the object hit
                Vector3 directionOfImpact = other.transform.position - transform.position;
                directionOfImpact.Normalize();
                directionOfImpact.x = Mathf.Round(directionOfImpact.x);

                //Find the direction this collider was going to apply force originally
                Vector3 currentForceDirection = new Vector3(Mathf.Cos(newHitAngle), Mathf.Sin(newHitAngle), 0);
                currentForceDirection.x *= directionOfImpact.x;

                //Find the new angle based on the direction of the attack on the x axis
                float dotProduct = Vector3.Dot(currentForceDirection, Vector3.right);
                newHitAngle = Mathf.Acos(dotProduct);

                //Find if the angle should be negative or positive
                if (Vector3.Dot(currentForceDirection, Vector3.up) < 0)
                {
                    newHitAngle *= -1;
                }
            }

            //Add the game object to the list of collisions so it is not collided with again
            Collisions.Add(other.gameObject);

            //Grab whatever health script is attached to this object
            HealthBehaviour damageScript = other.GetComponent <HealthBehaviour>();

            if (Owner)
            {
                OwnerName = Owner.name;
            }

            //If the damage script wasn't null damage the object
            if (damageScript != null)
            {
                damageScript.TakeDamage(OwnerName, _damage, _knockBackScale, newHitAngle, damageType, _hitStunTime);
            }

            onHit?.Invoke(other.gameObject, otherCollider);

            if (DestroyOnHit)
            {
                Destroy(gameObject);
            }
        }
        private void OnTriggerStay(Collider other)
        {
            //Only allow damage to be applied this way if the collider is a multi-hit collider
            if (!IsMultiHit || other.gameObject == Owner || !CheckHitTime())
            {
                return;
            }

            ColliderBehaviour otherCollider = null;

            if (other.attachedRigidbody)
            {
                otherCollider = other.attachedRigidbody.gameObject.GetComponent <ColliderBehaviour>();
            }

            if (other.CompareTag("ParryBox"))
            {
                return;
            }

            if (otherCollider && IgnoreColliders)
            {
                return;
            }

            else if (otherCollider is HitColliderBehaviour)
            {
                if (((HitColliderBehaviour)otherCollider).Priority >= Priority && otherCollider.ColliderOwner != ColliderOwner)
                {
                    Destroy(gameObject);
                    return;
                }
                return;
            }

            //Grab whatever health script is attached to this object. If none return
            HealthBehaviour damageScript = other.GetComponent <HealthBehaviour>();


            float newHitAngle = _hitAngle;

            //Calculates new angle if this object should change trajectory based on direction of hit
            if (_adjustAngleBasedOnCollision)
            {
                //Find a vector that point from the collider to the object hit
                Vector3 directionOfImpact = other.transform.position - transform.position;
                directionOfImpact.Normalize();
                directionOfImpact.x = Mathf.Round(directionOfImpact.x);

                //Find the direction this collider was going to apply force originally
                Vector3 currentForceDirection = new Vector3(Mathf.Cos(newHitAngle), Mathf.Sin(newHitAngle), 0);
                currentForceDirection.x *= directionOfImpact.x;

                //Find the new angle based on the direction of the attack on the x axis
                float dotProduct = Vector3.Dot(currentForceDirection, Vector3.right);
                newHitAngle = Mathf.Acos(dotProduct);

                //Find if the angle should be negative or positive
                if (Vector3.Dot(currentForceDirection, Vector3.up) < 0)
                {
                    newHitAngle *= -1;
                }
            }

            if (Owner)
            {
                OwnerName = Owner.name;
            }

            //If the damage script wasn't null damage the object
            if (damageScript != null)
            {
                damageScript.TakeDamage(OwnerName, _damage, _knockBackScale, newHitAngle, damageType, _hitStunTime);
            }

            onHit?.Invoke(other.gameObject);

            if (DestroyOnHit)
            {
                Destroy(gameObject);
            }
        }
 /// <summary>
 /// Copies the values in collider 1 to collider 2
 /// </summary>
 /// <param name="collider1">The collider that will have its values copied</param>
 /// <param name="collider2">The collider that will have its values overwritten</param>
 public static void Copy(ColliderBehaviour collider1, ColliderBehaviour collider2)
 {
     collider2.Init(collider1.DespawnsAfterTimeLimit, collider1.TimeActive, collider1.Owner, collider1.DestroyOnHit, collider1.IsMultiHit);
     collider2.OnHit          = collider1.OnHit;
     collider2.LayersToIgnore = collider1.LayersToIgnore;
 }