Search Unity

how do I play my animation through script?

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

  1. romans8710

    romans8710

    Joined:
    Mar 29, 2020
    Posts:
    7
    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

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,464
    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: https://docs.unity3d.com/Manual/class-Transition.html

    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

    jais_unity

    Unity Technologies

    Joined:
    Aug 19, 2017
    Posts:
    2
    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.
     
unityunity