Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

how do I play my animation through script?

Discussion in 'Animation' started by romans8710, Apr 21, 2020.

  1. romans8710


    Mar 29, 2020
    I just have a simple recoil animation that rotates the my gun upwards and downwards really quickly to make it seem as if there is recoil but I don't know how to trigger it through script when I shoot, any thoughts?
  2. Olmi


    Nov 29, 2012
    Hi @romans8710,

    Could you share a bit more details? Are you using Mecanim and Animator? As you didn't tell any details I'll guess that you are. In that case you would define a transition to the desired animation, then set a condition, usually a boolean or a trigger (or other type of value) that you use to switch to that animation.

    See the manual:

    Then you just script your code to set that trigger in your animation.
    Code (CSharp):
    1. myAnimator.SetTrigger("Recoil");
    jais_unity likes this.
  3. jais_unity


    Unity Technologies

    Aug 19, 2017
    you could use some code like this
    Code (CSharp):
    1.         if (Input.GetKeyDown(KeyCode.Space))
    2.         {
    3.             m_animator.SetTrigger("recoil");
    4.         }
    And then have the condition for going in to the state with your animation, set to be your trigger parameter.