Scripted Mecanim Animations in Unity

Sharing is caring:

Control Mecanim Animations with Javascript

In this video we’ll go over how to use Javascript to script player inputs with Mecanim animations. This will be the last video in this series using the MakeHuman model. We’ll start off with a blank animator window and create a new idle animation state with a new idle animation. Then we’ll add in a walking animation state that will be “activated” using a custom parameter that we’ll create.

The parameter will be called “Walk” and it will be activated once the vertical input is present. Creating a transition from the idle to animation state, and back again. Next we’ll add in a sprint animation and a new parameter called “Sprint” that will activate once we press our left click button. Both of these parameters will be float values.

After that we’ll show you how to setup a blend tree which will use a combination of three different animations to “blend” different movements together. In our case we will use a blend tree to combine a forward walking animation clip with a right and left animation clip to seamless blend the motions together.The animation clips themselves aren’t perfect, but we’ll manage to blend them in fairly well. We’ll then go over the scripting which is located below.


Software Used


Javascript File

#pragma strict

var animator : Animator; //stores the animator component
var v : float; //vertical movements
var h : float; //horizontal movements
var sprint : float;

function Start () {

animator = GetComponent(Animator); //assigns Animator component when we start the game


function Update () {

v = Input.GetAxis("Vertical");
h = Input.GetAxis("Horizontal");


function FixedUpdate () {

//set the "Walk" parameter to the v axis value
animator.SetFloat ("Walk", v);
animator.SetFloat ("Turn", h);
animator.SetFloat("Sprint", sprint);


function Sprinting () {
if(Input.GetButton("Fire1")) {
sprint = 0.2;
else {

sprint = 0.0;


Jonathan Gonzalez

I love all things in game development and want to contribute by teaching others how to create games. I'm always looking to help others create awesome digital worlds. I've been a hardcore gamer since I was a kid and now it's my turn to create awesome digital experiences.

More Posts - Website

Follow Me:


  1. hi.
    Very good tutorial, but i have a problem, my character it does not move , it stay in place (but the animation and the script are running great) Can You help me please?

    • I would check a few things:

      -Make sure the parameters used in your script are case sensitive and written exactly as you have in your animator controller
      -Look at the transitions, does anything happen when you try to walk? If nothing happens then it isn’t recognizing your parameters and/or script
      -Make sure the animator controller is actually being used in your script. When you play the game the animator controller should be in the animator variable slot in your script.

      I may test this out myself as a lot of people seem to have this issue so I need to see where the problem is, maybe something I’m not emphasizing enough. Let me know if you still have issues. I may create a C# version of this and upload the script as well.

  2. hi . thanks for the fast reply .
    when i play the transition and the other thing are working . the character just doesn’t move forward . it’s running on place .
    i hope you understand . thk again .

  3. hi, great tutorial, I follow every step but when I test in the game mode, when i press W or foward the character go to right…left side or move foward dont work, but in the animator window all respond perfectly. can you help me?

    • Make sure that your blend tree settings are setup properly. You probably have the left/right animations set to “0”. The walk forward animation should be set to 0 meaning it’s “neutral” with no horizontal movement. The left/right animations should be set to -1/1 to indicate moving to a positive horizontal movement or negative horizontal movement. Also make sure this blend tree is 1D and has the parameter for turning (called “turn”).

  4. Can you write a script for jumping and attacking please? 🙂

  5. Hi i am making a game where if a button (e) is pressed, the player does the run and jump animation, how would i do that code?

    • You’d do it the same way as any other animation. Just create a transition from the animation you want to go “from” and “to”. Create a transition between the two and use a parameter. If you’re using a jump animation a “trigger” parameter is probably best. If you’re using a single key input a trigger in general is best. The transition going back would just be set to “exit time” as you’d just want it to transition back after the animation clip has finished playing.

      For the code just use “If(Input.GetKeyDown(KeyCode.E)” then after that your animation parameter scripting would be included. So changing a parameter value ex.”anim.SetBool(“myParameter”, true)”.

  6. DevinDerps says:

    The tutorial is great, but I have a problem. When my character walks forward and stops it is perfect but when I move left or right then look around I go all over the place. Please help!

    • I’m not sure what you mean by look around. The camera in the tutorial is attached as a child object of the character, so any movements he makes the camera will go along with. Also what do you mean by “go all over the place”, do you mean the character starts moving around the scene, or the camera?

  7. Thank you so much I have learnt a lot with this tutorial. You are a great person sharing your knowledge with us.

  8. JoeyAwesome02 says:

    First off, I think your tutorials are great. They are really explanatory. But second off, I followed your tutorial completely but when the arrow keys are pressed he doesn’t move. Why? The walk parameters and thing work perfectly in the code.

    • Check your animation transitions, sometimes they overlap. A lot of people tend to overlook this part and forget to adjust the transitions between the two animation clips so it looks like nothing is happening but in reality it is working it just doesn’t look like it.

      • JoeyAwesome02 says:

        What do you mean by overlap? All of the parameters work and should be set up correctly in the animator. I just checked.
        Do I need an avatar to make the character move? Could that be causing the problem?

        • Overlap as in the animations clips. Parameters just allow us to transition, but if you don’t have the animation clips slightly overlapping to “blend” into each other then it’ll cause issues. Most of the time the animation clips are on top of each other which means neither is blending into each other.

      • JoeyAwesome02 says:

        I finally found out the problem! First off I had the wrong code. But I fixed that, and then I just went through the game a few times checking different things. I found that the animations where running at the right time, and they WERE running, but for some reason the player didn’t actually pull of the animations. Do you know why? Thanks, JoeyAwesome02

  9. Mudabbir says:

    sir u made my life so easy sir it will be very glade for me if u upload the tutorial of jump and other animations like in same scenarios plzzzz

  10. i cant seem to figure out how to fix this error i have searched online but was wondering if you could help

    Assets/scrips/Animator.js(24,10): BCE0019: ‘SetFloat’ is not a member of ‘Animator’.

    • I’m not sure why you’re getting that error, the script is fine and “SetFloat” is used when setting float values to parameters. Here is a link:

      The only thing I can think of is that you’re using Animator.SetFloat vs animator.SetFloat. make sure you’re using the variable name and not the variable type in that line.

      • So i figured out what the problem was …apparently it didn’t like the script being named Animator changed it to CharAnim and the script works fine now.. …. Thanks for your reply

        • It’s always best to avoid naming conventions similar to variable types that Unity might use. I’ve had that issue before and Unity tends to get confused and throw all sorts of weird errors. Glad you figured it out.

  11. youssef essmar says:

    I completed the script with melee so i write function Melee () {
    it gives me error what should i write i have made paramters and all good

    • Writing out a function name doesn’t do anything. If I wrote out “function Run()” it’s not going to have my character run. If you want your character to move you need to include transitions and parameters, then you need to change/enable those parameter values within those functions. Those functions need to work inside the Update function and/or be called through an animation even in order to check if anything has changed. If you notice the Fixed Update function is changing the parameters based on the Horizontal and Vertical axis inputs.

  12. Faisal Al Aadel says:

    Captin , from where you got this characters ! its Compatible with mocap animation !! i need it plz >

  13. i followed tutorial.when i run and press w the character is not walking and also if press the other keys viz a,d and s even then it is not walking or running.first of all it is recognising the keys ie w , s , a , d….i follwed exactly the video …could u tell what could be the issue…the character is always in idle state…it is not moving to other state….

  14. from where can i get male model pls….in asset store i didn’t find male as well as female models….could u guide from where can i get free models pls…..

  15. i have added one “cube” gameobject and javescript attached to it.
    function LateUpdate ()
    this.transform.position.z += this.speed;
    this.transform.position.z -= this.speed;
    this.transform.position.x -= this.speed;
    this.transform.position.x += this.speed;
    here i am unable to move the cube using “w , s ,a and d” keys .could u tell me what could be the issue.i just tested whether keys work or not.unfortunately keys are not working…pls guide me…

  16. i have created cube gameobject and javascript attached to it.
    function Update()
    print(“I am cube”);
    transform.Translate(0f , 1f*Time.deltaTime , 0f);
    on the console
    I am cube is getting displayed
    the cube is moving upwards.
    but if i press “a” and “d” keys they are not getting displayed.Instead UnityEngine.MonoBehaviour:Print(object) is getting displayed.why the keys “a” and “d” keys are not getting displayed when i press “a” and “d” keys on the keyboard.Is that because of unity5.1.1f1 free version?

    • If you want to display info from the horizontal axis just use “Debug.Log(Input.GetAxis(“Horizontal”));” or you can also make it more clear by adding “Debug.Log(“Horizontal value is ” + Input.GetAxis(“Horizontal”));”

  17. Help my character won’t move

    And I read all your comments when helping people with this issue it still doesn’t help

    Everything is working propperly I checked eveyrthing
    She just stands there

    • That doesn’t really give me a lot of information. Does your character not move at all, even after a set time? Have you looked at the animator states while trying to move, do the transitions happen? Is your script referencing the correct parameter? Remember parameters are case sensitive. Some things to check:

      – Make sure the idle to walk state does not have “has exit time” checked on, 99% of the time this is something people forget
      – Check the animation clips, are they overlapping? One must end where the other starts
      – Is your script working properly? Does the function for calling for the parameter work?

  18. Hi thank you for the excellent tutorial. been trying to get a character running for a while. I done all the steps verbatim but I keep getting the error unknown identifier ‘GetComponet’ .
    I changed my button to the shift key?

    #pragma strict

    var animator : Animator;
    var v : float;
    var h : float;
    var sprint : float;

    function Start () {

    animator = GetComponet(Animator);


    function Update () {

    v = Input.GetAxis(“Vertical”);
    h = Input.GetAxis(“Horizontal”);


    function FixedUpdate () {

    animator.SetFloat (“Walk”, v);
    animator.SetFloat (“Turn”, h);
    animator.SetFloat (“Sprint”, sprint);


    function Sprinting () {
    if(Input.GetKeyDown(“shift”)) {
    sprint = 0.2;
    else {

    sprint = 0.0;


  19. Hi Jonathan, great tut! Just two questions though. When I add the Js to the character I also get the Animator field which is currently assigned to none. This is because I cannot see it on your video. Can you tell me why? Maybe I am missing something. Also, when I press the Sprint button (left click) it does work but won’t change the direction. In fact it keeps sprinting (running) even when the button is released. On your tut it works though and I do not understand where I am failing. Any help?

    • Well the script is currently setup so that it automatically assigns the Animator component, but the script needs to be applied to the object that has the Animator component on it otherwise you need to assign it yourself and/or tell the script what object actually has the component on it. The Sprinting is not set to change direction, it’s just based on sprinting forward. In order to sprint and turn you’d need to use something like a 2D Blend tree. 1D blend trees are limited in that sense. You could also use a 1D Blend tree with sprinting animations just like the walking/turning animation blend tree although that seems a bit much when a 2D blend tree can do it all. If he’s still sprinting after you left go you need to ensure a few things, one that you’re using “GetButton” and not “GetButtonDown” the former is used to hold button inputs while the latter is used if you click it once. The second thing is you need to ensure that the sprint transition is working properly. Make sure that the transition back from sprinting is set to below .02 so that it knows you’ve let go of the button and the value is now below .02 meaning you’re no longer sprinting.

  20. thank you so much actually it is working …….
    I just want to ask you how to increase the speed of the animation????………………….
    I hope you will help me……….
    waiting for your reply……………..

    • Click on the animation state from the Animator window. On the right (in the inspector panel) you’ll see an option for speed. 1 is the default, higher than that and you increase the speed of the animation.

  21. When the game starts, the console says “Parameter ‘Sprint’ does not exist.” Why is that? The idle animation plays, but that’s all.

    • There are a few issues when you see that appear. First, the “Sprint” parameter was created within the Animator controller, if you don’t have that and the script does that’s why you’d have that pop up. If your idle animation is the only thing playing, even when trying to move around, something is wrong with either your script or your transitions. 99% of the time the issue is because you have “exit time” checked on in the transition which will wait until the animation finishes playing before transitioning to the next state.

  22. My Characher can only move forward. It can’t move neither left nor right. It can’t sprint too!
    My script, and animation looks so much identical to what you’ve done.

    • I would check your animation transitions to see if they are actually being used when you try to turn or sprint. Make sure you uncheck “has exit time” on the animation transitions otherwise the animation will continue to play until it has completed before transitioning.

  23. Jagwinder says:

    Hi I followed your video but my character doesn’t move also it does not go to any state it remains in t pose when I click up arrow or w it does not move

    • It’ll be one of three things. You don’t have an animator controller assigned to the animator component of the character, your parameters are not being used in your script properly (case sensitive names), or you have exit time enabled on your transitions. Most of the time it’s the last one that people always seem to forget. Exit time means the animation will play in it’s entirety before transitioning so it looks like it’s not working, but really it’s just waiting for the animation to finish.


  1. […] tutorial I’ll break down the basics of scripting Mecanim animations. In a previous tutorial, Script Mecanim Animations, I had a lot of questions regarding common errors and issues when trying to follow along. In that […]

  2. […]…In this video we'll go over how to use Javascript to script player inputs with Mecanim animations. This will be the last video in this series using the MakeHuman model. We'll start off with a blank animator window and create a new idle animation state with a new idle animation. Then we'll add in a walking animation state that will be "activated" using a custom parameter that we'll create. […]

  3. […] In this video we’ll go over how to use Javascript to script player inputs with Mecanim animations. This will be the last video in this series using the MakeHuman model. We’ll start off with a blank animator window and create a new idle animation state with a new idle animation. Then we’ll add in a walking animation state that will be “activated” using a custom parameter that we’ll create. […]

Speak Your Mind