コード例 #1
0
    /// <summary>
    /// On collision with the enemy then check object tag and match with enemy.
    /// </summary>
    /// <param name="collision"> Default collision component.
    /// </param>
    private void OnCollisionEnter(Collision collision)
    {
        // if an enemy is collided.
        if (collision.gameObject.CompareTag("Enemy"))
        {
            // asssign the collided object to the current object.
            cObject = collision.gameObject;
            // detect the player from the enemies side.
            PD = cObject.GetComponentInParent <PlayerDetection>();
            // reduce health.
            health.takeDamage(PD.damage);
            // Destroy the script component.
            PD.isDestroyed();
            PD      = null;
            cObject = null;
        }

        // if collision is with an enemy projectile then take damage.
        if (collision.gameObject.CompareTag("Projectile"))
        {
            cObject = collision.gameObject;
            PJB     = cObject.GetComponentInParent <ProjectileBehaviour>();
            health.takeDamage(PJB.damage);
            PJB.IsDestroyed();
            PJB     = null;
            cObject = null;
        }
    }
コード例 #2
0
 /// <summary>
 /// Function that triggers when the player is continuing to collide with an enemy or a projectile
 /// </summary>
 /// <param name="collision">  Default collision component.
 /// </param>
 private void OnCollisionStay(Collision collision)
 {
     if (collision.gameObject.CompareTag("Enemy"))
     {
         cObject = collision.gameObject;
         PD      = cObject.GetComponentInParent <PlayerDetection>();
         health.takeDamage(PD.damage);
         Debug.Log("Collided with enemy, damage dealt is :" + cObject.name);
         PD.isDestroyed();
     }
     if (collision.gameObject.CompareTag("Projectile"))
     {
         cObject = collision.gameObject;
         PJB     = cObject.GetComponentInParent <ProjectileBehaviour>();
         health.takeDamage(PJB.damage);
         PJB.IsDestroyed();
         PJB     = null;
         cObject = null;
     }
 }