Animator Scripting – Unity Official Tutorials

Animator Scripting – Unity Official Tutorials

  04 Oct 2019   , ,

In this lesson we will be addressing some of the more common uses of the animator class. For complete documentation on the animator class see the documentation linked below. In order to affect transitions between the states of an animator controller we need to set the animator parameters to appropriate values. To do this we need to use some of the functions of the animator class. Here we have a scene with a model Ethan in it. Ethan has an animator component and a script called Ethan Script attached. The animator component is using a controller named EthanController. If we open EthanController we can see that there are 4 states. Idle, Walk, Run and Jump. Additionally there are 2 parameters. A float named Speed and a trigger named Jump. the controller is setup in such a way that Ethan will transition from Idle to Walk to Run based on the Speed parameter. Furthermore, if the Jump parameter is triggered while Ethan is running Ethan will enter a Jump state. All of the animations of the Ethan model are controlled by these 2 parameters, which we will now learn to control with our Ethan script. Inside the Ethan Script we first need to create an animator variable named anim to contain our animator object. We get a reference to this animator object in the start method by typing anim=GetComponentFor further details on the GetComponent function see the lesson linked below. Are animator controller is setup so that the Speed parameter will dictate which moving animation is being played. We can use the player’s input to control that behaviour. The first thing we will need to do is create a float variable which we will call Move in the update method. Then we will set the Move variable equal to the vertical input axis by typing Input.GetAxis and in parenthesis we will parse the string Vertical. Next we will want to parse the value of the Move variable to the Speed attribute of the animator controller. This is done with the SetFloat function of the animator class. We are using SetFloat because Speed is a float parameter. If it had been a Boolean parameter we would use SetBool. If had been a trigger parameter we would use SetTrigger and so on. For each of these functions there are multiple ways that they can be called. The most common way is to first parse in the name of the parameter you are setting as a string. Then the next item is the value you wish to set that parameter to. In this instance we will call the SetFloat method of our animator variable by typing anim.SetFloat and then in parenthesis the string Speed and our variable Move. Back in Unity we can run our scene. We can see that if we press the W key or the up arrow our model now moves. The Speed parameter and the movement animation played is now controlled by the player. Parsing in a string name for the parameter we wish to set can be inefficient because it means that the animator needs to evaluate the string every time it is called. A faster way of setting parameter values is by using hashIDs. Each string that an animator can use is given a hashID. This is an integer value that is unique for every different string. We can store this value and then set parameters using this ID instead. To do this, first we will need to store the value. Let’s create an integer variable named jumpHash to store the hashID in. We get the value for our hash variable by calling the static method of the animator class string to hash. Since this method is static we access it by typing Animator.StringToHash. In the parenthesis we parse in the string we wish to know the hashID value of. In order to make our model jump we will need to set the Jump trigger parameter in the animator controller. In our case we want to make the model jump whenever we press the space bar. So we will start by typing if Input.GetKeyDown and then in the parenthesis we will use KeyCode.Space. Inside our if statement we will set the Jump trigger parameter using the anim.SetTrigger method. Inside the parenthesis we will parse our jumpHash variable. Remember that this variable contains the hasID of our Jump parameter. Note that since Jump is a trigger we do not need to also parse in a value. Back in Unity we can run our scene. Notice that while we are running we can also press the spacebar to jump. We have now utilised both our float parameter Speed and our trigger parameter Jump to make our model more dynamic. Occasionally, when writing the logic behind animation we may want to find out what state the controller is in. Let’s say for example we want to be able to set the Jump trigger only when the model is in the Run state. The animator has some functions that we can call to find out what state the controller is currently in. We can do this by creating an animator state info variable which we will call StateInfo. The animator state info class stores the name, name hash, and various other pieces of information about a state. We will set our state info variable equal to the anim.GetCurrentAnimatorStateInfo function. Inside the parenthesis we will parse the index of the layer we want to know more about. Since we are dealing with the base layer we will parse in a 0. Again we could compare the current animation states name with a string but since it is more efficient to compare hashIDs let’s do that instead. We will start by creating an integer named runStateHash and setting it equal to our Animator.StringToHash function. We will parse in to the parenthesis the string Base Layer.Run We have to include the base layer. part because we can have states of the same name on different layers. Now we can modify our if statement to also only allow our model to jump if stateInfo.nameHash equals our runStateHash variable. Subtitles by the community

69 thoughts on “Animator Scripting – Unity Official Tutorials

  1. I have tested a few different game development programs but never got anything done. But when I got to Unity, that all changed. It is the greatest program I ever tested and used! Also, with the free version, you can do anything just as the pro version but the pro version do have a few awesome features too! And with these official tutorials, it is the easiest program to learn!

    Thanks Unity! 

  2. unity is pretty, uniting beauty with logic, a mixture of comfort and performance just like mercedes car…. i love its beauty through my mind ….just thought to describe how i feel about unity

  3. why is it that some versions of unity simply allow you to do animation.Play"animation"); while it seems the newer one forces us to use the long process…?

  4. mlnodevelop scripting refuses to play my animations. it keeps saying my animations need to be marked as Legacy, something I do not know how to fix. furthermore, it keeps telling me I have no character controller attached, and I don't see how that can be. Claryfying some of these errors would be very helpful

  5. "Parameter 'Speed' does not exist'
    How does it not exists?  Am I doing it wrong or is there a new name for the parameter?

  6. yes, I myself have written a different code still worked example function start(){
    } wrapmode = wrapmode.once;       function fixedupdate(){
    }"attack");        if(ınput.getkeydown("e")){
    }               I wrote them myself, this code works

  7. If I have a default state of "Idle" and I want walk to be able to interrupt it like in your video, how do I get it to do that? It just waits until Idle is done.

  8. Can you make something even simpler than this..? Like having an Idle animation on startup, play a jump animation when pressing the Space Bar, then return to the Default idle animation? 

    I tried all that you're describing right here and it's jump playing both the Idle and Jump animation upon startup.. I checked your site's documentation and looked at the Unity help, nothing seems to be helping with the issue I'm having.

  9. my animtions are playing only once rather than repeating the same actions. And how do you set up  the character controller so that when press a key it plays the animationsit has been assigned to?

  10. When I put this following line to java script than it say me to put a semicolumn at (4,30) and if I add a semicolumn then it shows a lot more errors. If you can fix this line then please fix this line.

    AnimatorStateInfo state = animator.GetCurrentAnimatorStateInfo(0);

    and the whole script is here:)

    #pragma strict

    var animator : Animator;

    function Start ()
    animator = GetComponent(Animator);

    function Update ()
    AnimatorStateInfo state = animator.GetCurrentAnimatorStateInfo(0);

    if(Input.GetButton ("Fire1")) animator.SetBool("Jump", true);

    if ( == "Base Layer.JumpUp")
    animator.SetBool("Jump", false);
    animator.MatchTarget(RightHand.position, RightHand.rotation, AvatarTarget.RightHand, animator.GetFloat("MatchStart"), animator.GetFloat("MatchEnd"), new Vector4(1, 1, 1,0));

  11. hey i am making an pokemon game and wanted you to help me in doing the pokemon catching thing and pokemon in and out of the ball. please please make an video on it.

  12. Yes
    void Update()
    transform.position += transform.forward * Time.deltaTime * speed;
    GetComponent<Animation> ().CrossFade ("Walk");

  13. How do I set up the connections?
    How do I set up the states like "Jump"
    Where do I find tutorials that don't start 60% of the way through the damn process?

  14. Hi to every one!
    I am learning Unity and blender and I had got alot.
    Here I come towards you to ask, for programing which language should i learn C# or Javascripts?
    I already know a lattle bit about Java.
    So what do you suggest to me?

  15. Sorry, but I'm a beginner with Unity as well. How do you temporarily disable the horizontal and vertical function when you hit the input Jump, so the character doesn't slide when the player uses the direction keys/d pad

  16. my animations play and update but the position wont??
    my character just stays in the same position?

    any thoughts?

  17. I'm using anim.SetInteger with animpar values and when I tried your method for introducing a jump button it doesn't work, I get the parsing error :/

  18. Why it not working. i am following your script word for word and yet it just sais parsing error and highlights the entire script.

  19. +Unity please tell me how to create that 'Ethan Script' Component.
    I tried to add that component but that script does not show up.

  20. I have problem. I made model in Blender and made animation for this, but when I try to animate it in Unity, I need to transform its position via scripts. Where I can good tutorial how to make models, or how to resolve this problem? Its nice for simple animations liek walk or run, but its hard to conrol with more complex moves.

  21. I can't even begin to understand why you would send out tutorial videos for the most basic aspects of your product only to have them not work at all when followed line for line… This is outdated and should be removed or annotated. Your "tutorial" made me waste 2 hours and pissed me off. Great job.

  22. as usual the unity tutorial is useless i would recomend looking around youtube, not only is it 3 year outdated but it is also horible

  23. didnt work for me, i felt like there is 50% missing from this tutorial? it wasnt specified if this ONLY works for a character with "apply root motion" or not. the move variable appears to be something i need to setup? i came here for answers and ended up with more questions

  24. What the fuck are you people talking about? His voice is professional.
    I'm having trouble to understand other guys just passing by topics while I must translate it in my mind.
    This guys is calm and clear, nothing much more to expect.

  25. Your tutorials are life savers, thanks for the awesome work.

    Most of times I just get pieces of them to get the idea so not sure if it is useful for the main purpose (based on other's comments).

  26. You don't even explain what is in each of the animation boxes for walk, run, and jump. How the heck can this script work if we don't even have the proper animator to use?

    What the fuck is a variable!? Do i seriously have to go back and watch all the tutorials again. I can't listen to this fuckhead take 10 second pauses after every god damn sentence!!!!!

  28. I'm getting error in unity 2018.3 help guys
    using UnityEngine;

    public class Test : StateMachineBehaviour 



        public float m_Damping = 0.15f;

        private readonly int m_HashHorizontalPara = Animator.StringToMesh ("Horizontal");

        private readonly int m_HashVerticalPara = Animator.StringToMesh ("Vertical");

        override public void onStateUpdate(Animator animator, AnimatorStateInfo stateinfo, int layerIndex)


            float Horizontal = Input.GetAxis ("Horizontal");

            float Vertical = Input.GetAxis ("Vertical");

            vector2 input = new Vector2 (horizontal, vertical).normalized;

            animator.SetFloat(m_HashHorizontalPara, input.x, m_Damping, Time.deltatime);

            animator.SetFloat(m_HashVerticalPara, input.y, m_Damping, Time.deltatime);




  29. "Checked with: 4.3". This is still linked to on the website despite being years out of date. Get it together.

  30. Bir şey soracaktım unity de import yapıp aldığımız ücretsiz grafikleri ilerde oyun yapıp piyasaya sürdüğümüz zaman telif hakkı alır mıyız?

  31. what are the methods, which return you, the length of the current frames or if I want the animation or playback speed to be detected at some point in the animation?????????????????????????????????????????????????

Leave a Reply

Your email address will not be published. Required fields are marked *