Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Help Wanted Setting "Is Trigger" to an enemy AI's mesh collider cancels the attack animation

Discussion in 'Animation' started by CapruxSolder11, Mar 4, 2021.

  1. CapruxSolder11

    CapruxSolder11

    Joined:
    Jan 15, 2017
    Posts:
    1
    Hey, all. I'm running into this issue in my game where the enemy can attack when in range and stop when out of range, but the weapon doesn't actually collide with the player without a trigger on the mesh collider (the same way I have it rigged vice versa for the player's weapon has a trigger and the animation runs fine), but when you add the trigger, the attack animation just doesn't happen anymore. If I were to add a rigidbody to the weapon and just make it a collision rather than a trigger, the same thing happens - no animation. When the trigger is added, yes, the player can now interact with the enemy's weapon, but the enemy doesn't actually swing at the player like they are supposed to. I have the playerController and enemy script shown here
    Code (CSharp):
    1.  private void OnTriggerEnter(Collider other)
    2.     {
    3.        
    4.          if (other.gameObject.CompareTag("Enemy Weapon"))
    5.          {
    6.           Debug.Log("BIG OOF");
    7.           playerHealth -= MoveToPlayer.enemyDamage;
    8.          }
    9.        
    10.  
    11.  
    12.         if (other.gameObject.CompareTag("Powerup"))
    13.         {
    14.                 Destroy(other.gameObject);
    15.                 playerHealth += 25;
    16.         }
    17.        
    18.     }
    19.  
    20.    
    21.  
    22.  
    23.    
    24.  
    25.     public void PlayerDeath()
    26.     {
    27.         if (playerHealth <= 0)
    28.         {
    29.             playerAnim.SetBool("Death", true);
    30.             Debug.Log("YOU HAVE PERISHED. PLAY AGAIN?");
    31.             gameOver = true;
    32.         }
    33.     }
    34.  
    35. and the enemy's script is as follows:
    36.  
    37. [code=CSharp]private void OnCollisionEnter(Collision collision)
    38.    {
    39.        if (isDead == false)
    40.        {
    41.            if (collision.gameObject.CompareTag("Player"))
    42.            {
    43.                withinRange = true;
    44.                AttackCycle();
    45.              
    46.            }
    47.        }
    48.  
    49.      
    50.    }
    51.  
    52.    private void OnCollisionExit(Collision collision)
    53.    {
    54.        withinRange = false;
    55.        //attackToggle = false;
    56.    }
    57.  
    58.    void AttackCycle()
    59.    {
    60.      
    61.      
    62.        if (withinRange == true)
    63.        {
    64.            //attackToggle = true;
    65.            enemyAnim.SetTrigger("Attack_trig");
    66.            //ToggleDelay();
    67.        }
    68.    }
    69.  
    70.    //IEnumerator ToggleDelay()
    71.    //{
    72.        //yield return new WaitForSeconds(1f);
    73.        //attackToggle = false;
    74.    //}

    Getting rid of the trigger lets the enemy play out the animation just fine, but as soon as the trigger is added, no dice. Everything I've found online seems pretty irrelevant to this situation, so maybe you guys can help? Thanks a bunch.
     
unityunity