Search Unity

How can i activate an animation when a button is pressed?

Discussion in 'Scripting' started by EndyCodes, Aug 3, 2019.

  1. EndyCodes

    EndyCodes

    Joined:
    Apr 18, 2016
    Posts:
    9
    So im making a script wherein if a object is collided with the player, the player does a new animation,



    then i want to do if the first condition is fufilled if a button is pressed then the animation plays, ive got the code down generally


    Code (csharp):
    1.  
    2.  
    3. private void OnTriggerEnter2D(Collider2D other)
    4. {
    5. if (other.gameObject.name == "Player" && (Input.GetKey("l")))
    6. {
    7. myanim.SetBool("coll", true);
    8.  
    9.  

    How do i make it so if the game object collides and then later if L is pressed then play the animation?
     
  2. Callz01

    Callz01

    Joined:
    Apr 9, 2019
    Posts:
    16
    Code (CSharp):
    1.     public GameObject player;
    2.  
    3.     private bool hasCollided = false;
    4.  
    5.     void Update ()
    6.     {
    7.         if (hasCollided && Input.GetKeyDown(KeyCode.L))
    8.         {
    9.             player.GetComponent<Animator>().SetBool("coll", true);
    10.         }
    11.     }
    12.  
    13.     private void OnTriggerEnter2D(Collider2D other)
    14.      {
    15.         if (other.gameObject.name == "Player")
    16.         {
    17.             hasCollided = true;
    18.         }
    19.     }
    Also, "GetKey" is triggered if the user holds down the key. "GetKeyDown" is triggered when the user starts pressing down the key.

    Edit: Remember to drag your player object onto the empty gameobject field in the inspector.
     
    Last edited: Aug 3, 2019
  3. EndyCodes

    EndyCodes

    Joined:
    Apr 18, 2016
    Posts:
    9
    Thank you!
     
    Callz01 likes this.
  4. Callz01

    Callz01

    Joined:
    Apr 9, 2019
    Posts:
    16
    Your welcome :)
     
  5. EndyCodes

    EndyCodes

    Joined:
    Apr 18, 2016
    Posts:
    9
    Sorry, but it doesnt work, it triggers on scene playing, do you know how to fix it?
     
  6. Callz01

    Callz01

    Joined:
    Apr 9, 2019
    Posts:
    16
    Sorry, what do you mean?
     
  7. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    4,265
    Check your animation state machine, you probably transition from "Entry".
    https://docs.unity3d.com/Manual/Animator.html
     
    Callz01 likes this.
  8. EndyCodes

    EndyCodes

    Joined:
    Apr 18, 2016
    Posts:
    9
    The animation isnt linked to entry. I put a debug.log when i collide, but that doesnt show up, the animation just loops
    Continually. I dunno how to stop this


    upload_2019-8-3_18-5-15.png
     
  9. Callz01

    Callz01

    Joined:
    Apr 9, 2019
    Posts:
    16
    Which animation has the problem?
     
  10. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    4,265
    On the left hand side, is your "coll" bool checked in this window?
    Also check your transitions if you set them up correctly.
     
  11. EndyCodes

    EndyCodes

    Joined:
    Apr 18, 2016
    Posts:
    9
    oh wait, i figured it out, so i set transitions between then using coll as the variable, so now it works when i press the button, but it wont stop?
     
  12. Callz01

    Callz01

    Joined:
    Apr 9, 2019
    Posts:
    16
    Is looping unchecked on the animation?

    Edit: In the code, after the button has been pressed set the bool "hasCollided" back to false. Try that.
     
    Last edited: Aug 3, 2019
    EndyCodes likes this.
  13. EndyCodes

    EndyCodes

    Joined:
    Apr 18, 2016
    Posts:
    9
    yup that did it, should've figured that... Thank you again. if this was reddit i'd gild you or something
     
  14. Callz01

    Callz01

    Joined:
    Apr 9, 2019
    Posts:
    16
    Great! Your welcome :)