Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How can I stop shooting the enemy when it plays a "dying" animation?

Discussion in '2D' started by Yawuar, Mar 17, 2015.

  1. Yawuar

    Yawuar

    Joined:
    Mar 10, 2015
    Posts:
    6
    Hello

    I have a little problem.

    When I kill my enemy it plays a "dying" animation and it add 10 points for each enemy.
    The problem is when my enemy dies I can still shoot it and adds 10 point every time I hit it.
    How can solve it?

    You have to excuse me for my bad English :/
    Thanks.

    My code :

    Code (CSharp):
    1. public void OnTriggerEnter2D(Collider2D col) {;
    2.         if(col) {
    3.             Health -= 1;
    4.             Destroy(col.gameObject);
    5.             if(Health <= 0) {
    6.                 enemy_anim.SetBool("dying", true);
    7.                 isAlive = false;
    8.                 StartCoroutine(Die());
    9.                 ScoreManager score = levelManage.GetComponent<ScoreManager>();
    10.                 score.Score += 10;
    11.             }
    12.         }
    13.     }
    14.  
    15.     private IEnumerator Die() {
    16.         AnimatorStateInfo info = enemy_anim.GetCurrentAnimatorStateInfo(0);
    17.         yield return new WaitForSeconds(info.length + 1f);
    18.         Destroy(gameObject);
    19.     }
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Code (csharp):
    1. if(Health <= 0 && isAlive) {
     
  3. rogueknight

    rogueknight

    Joined:
    Feb 16, 2014
    Posts:
    44
    Exactly what Dantus said. You want to create a Boolean value that knows if your enemy is currently dying or not. Whenever the enemy gets hit by your shot set your Boolean "IsAlive" to false. Then in the collision code check and see if the enemy is currently alive or not.
     
  4. Yawuar

    Yawuar

    Joined:
    Mar 10, 2015
    Posts:
    6
    It works. Thank you guys!