how do I play my animation through script?

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?

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: Unity - Manual: Animation transitions

Then you just script your code to set that trigger in your animation.

myAnimator.SetTrigger("Recoil");
1 Like

you could use some code like this

        if (Input.GetKeyDown(KeyCode.Space))
        {
            m_animator.SetTrigger("recoil");
        }

And then have the condition for going in to the state with your animation, set to be your trigger parameter.