Ejemplo n.º 1
0
    //When the ai collides wtih something while falling through the air
    public virtual void OnCollide(ControllerColliderHit hit)
    {
        if (m_Active == true)
        {
            GameObject hitObject = hit.gameObject;

            //CHeck if we hit darkness
            if (hitObject.tag == Constants.DARKNESS_STRING || hitObject.tag == Constants.MOVING_PLATFORM_TAG_STRING)
            {
                Destructable destuctable = GetComponentInParent <Destructable>();
                if (destuctable != null)
                {
                    destuctable.instantKill();
                }
            }

            //Ignore stopping velocity from collisions with enemies
            if (hitObject.CompareTag(Constants.ENEMY_STRING))
            {
                BaseKnockedBackBehavouir knockedBack = hitObject.GetComponentInChildren <BaseKnockedBackBehavouir>();

                //Do not bother colliding with already flying enemies
                if (knockedBack == null || knockedBack.isActive())
                {
                    return;
                }

                //If the enemy is goomba stomping another enemy
                if (Vector3.Dot(hit.normal, Vector3.up) > 0.5f)
                {
                    //This enemy slows down after the collision
                    m_Velocity *= -ONCOLLISION_SLOW_SELF_MULTIPLIER;
                    return;
                }

                //If we have collided head on with another enemy

                //Move away from the enemy
                m_Controller.transform.position -= m_Velocity * Time.deltaTime;

                //The enemy collided with is knocked back with a slower speed
                knockedBack.SetKnockBack(m_Velocity.magnitude * ONCOLLISION_SLOW_ENEMY_MULTIPLIER / knockedBack.getLaunchMultiplier(), m_Velocity.normalized);

                //This enemy slows down after the collision
                m_Velocity *= ONCOLLISION_SLOW_SELF_MULTIPLIER;
            }
            //If the enemy collides with a player
            else if (hitObject.CompareTag(Constants.PLAYER_STRING))
            {
                //The enemy falls
                if (m_Velocity.y > 0.0f)
                {
                    m_Velocity.y = -0.1f;
                }
                m_Velocity.x = 0.0f;
                m_Velocity.z = 0.0f;
            }
            //If the enemy has collided with the kill zone
            else if (hitObject.name == Constants.KILLZONE_STRING)
            {
                m_Active = false;
                GetComponentInParent <Destructable>().instantKill();
            }
            //If the enemy has hit the floor
            else if (Vector3.Dot(hit.normal, Vector3.up) > ANGLE_FOR_COLLIDE_WITH_GROUND)
            {
                if (m_Velocity.y > 0.0f)
                {
                    return;
                }

                //Inactivate the character controller for now
                m_Controller.enabled = false;
                m_Active             = false;
                m_Velocity           = Vector3.zero;

                //Return the enemy to the nav mesh
                GetAgent().enabled = true;
                m_EnemyAI.SetState(EnemyAI.EnemyState.Idle);
            }
            //The enemy has hit a wall
            else if (m_Velocity.y > 0.0f)
            {
                m_Velocity.y = -0.1f;
            }
        }
    }