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

Question Animation doesn´t reset after playing it

Discussion in '2D' started by Zemox, Jul 6, 2020.

  1. Zemox

    Zemox

    Joined:
    May 6, 2020
    Posts:
    9
    So I set up a spike trap in my game this is probably a very simple problem but after colliding with the spike trap once it doesn´t reset so it only plays once.
    This is my code help would be appreciated
    Code (CSharp):
    1. public class SpikeTrapController : MonoBehaviour
    2. {
    3.     [SerializeField]
    4.     public Animator anim;
    5.  
    6.     private void OnTriggerEnter2D(Collider2D other)
    7.     {
    8.         if (other.CompareTag("Player"))
    9.         {
    10.             anim.SetBool("spikeGo", true);
    11.         }
    12.     }
    13. }
    14.  
     
  2. Mooxe

    Mooxe

    Joined:
    Sep 18, 2013
    Posts:
    19
  3. Zemox

    Zemox

    Joined:
    May 6, 2020
    Posts:
    9
    When I set loop time to true it just keeps playing and never stops.
     
  4. Mooxe

    Mooxe

    Joined:
    Sep 18, 2013
    Posts:
    19
    Hmm, just re-read your first question, I guess you may need to add a function to setBool of spikeGo to false when you exit the Trigger.
    Code (CSharp):
    1.   private void OnTriggerExit2D(Collider2D other)
    2.     {
    3.         if (other.CompareTag("Player"))
    4.         {
    5.             anim.SetBool("spikeGo", false);
    6.         }
    7.     }
     
  5. Zemox

    Zemox

    Joined:
    May 6, 2020
    Posts:
    9
    Hey thanks for the answer but this doesnt do the thing I want I have figured out the answer on my own you have to add a OnTriggerStay2D and a OnTriggerExit2D function, when only adding OnTriggerExit2D the animation suddenly stops when leaving the collider