Mecanim Tips: Basic Walk Scripting

Sharing is caring:

Looking for a Simple Mecanim Walking Script?

In this Unity 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 tutorial I covered some more intermediate Mecanim aspects such as using a 1D blend tree. Scripting in Mecanim can seem a bit confusing at first, and even more so when you throw in blend trees. I decided to get back to the basics of creating a very simple walking animation.

For this we’ll use a rigged character provided to you in the project files below. This is a rigged character created in MakeHuman and it properly setup for Mecanim. I also included an empty Animator Controller which is already assigned to the Animator Component of the character. With all that done we can then focus on the animations and the scripting aspect alone.

The project files are linked below.

Project Files

Raw Mocap Data – Unity Asset Store

 

 

 

 

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:
TwitterYouTube

Comments

  1. Hi JONATHAN GONZALEZ ,
    My goal is human should walk straight forward when i press “w” on the keyboard , and when i press “s” ,human should turn left and walk , when i press “d” human should turn right and walk.
    human has to sit on the chair and should work on the keyboard by keeping both the hands on the keyboard , and fingers of both the hands should move.how to achieve to this goal pls guide me.

    HumanAnimation javascript:

    var anim:Animator;
    var vert:float;
    var horz:float;
    var right:float;

    function Start () {
    anim = GetComponent(Animator);
    }

    function Update () {
    vert = Input.GetAxis (“Vertical”);
    horz = Input.GetAxis (“Horizontal”);
    //right = Input.GetKey (“D”);

    anim.SetFloat (“walk”, vert);
    anim.SetFloat (“turnright” , horz);
    //anim.SetFloat (“turnright” , right);
    }

    AnimController:

    1) Idle——————->WalkFWD (walk>0.1)
    2) WalkFWD———–>Idle(walkWalkForwardTurnRight_NtrlMedium(turnright>0.1)
    4)WalkForwardTurnRight_NtrlMedium———–>Idle(turnright<0.1)

    when i press "d" on the keyboard human turns right and walks , and when leave the "d" key , human comes to idle state by using
    3 & 4 statements

    human should turn left and walk …….when i press "a" on the keyboard…..how to achieve this task
    After " turning right and walking " or turning left and walking" the human sit on the chair , and operate on the keyboard by moving the fingers of both the hands…….how to achieve this task..
    pls guide me

    • A lot of this was already covered in the video. There’s also an updated version here: http://www.xenosmash.dreamhosters.com/mecanim-tips-1d-blend-trees/

      Regarding the sitting on the chair animations that’s really specific. It doesn’t say whether or not he’ll sit after a specific time or event so I can’t really tell you much regarding that. If you want your character to sit in a chair you’d probably just use a trigger near the chair then enable the sitting animation once you’ve pressed a specific button within the trigger.

  2. http://www.xenosmash.dreamhosters.com/mecanim-tips-1d-blend-trees/
    In the above said video the human turns left or right when he is in walking state.But my requirement is he should walk from idle state , he should turn left and walk from idle state , and he should turn right and walk from idle state i mean
    idle———>walk straight(when i press “w” key) and when i leave “w” key should come to idle state
    idle———->turn left and walk(when i press “a” key) and when i leave “a” key should come to idle state
    idle———->turn right and walk(when i press “d” key) and when i leave “d” key should come to idle state
    from where do i get sitting animation,
    how to use trigger near the chair,could u give one example pls

    • He does go from walk to idle, the rest of it uses a blend tree which requires him to walk and turn at the same time. If you want to go directly from turning to idle then you’d need to keep things separate by creating a walk, and two turning animation states (no blend tree). Then just create transitions from each going back to idle just like we did with the blend tree.

      This is how you would create a triggered animation. Can’t really give you any specifics on using it with a character as that would all be based on the animation you create.

  3. sridevipindi says:

    Hi jonathan
    i have created human character which walks , turns left , right and walks backward…and i have two more game objects viz table and chair…when the human reaches either table or chair he should stop walking , but instead he is walking through the objects.could u tell me how to stop human from walking through either table or chair when he reaches these two objects

  4. sridevipindi says:

    i am using walkforward , walkbackward , turnleft , turnright and idle fbx foramts.from idle state to walkforward by pressing “w” key or up arrow key
    from idle state to backward by pressing “s” key or down arrow key
    from idle state to turnleft by pressing “a” key or left arrow key
    from idle state to turnright by pressing “d” key or right arrow key

    • Is this a question?

      • sridevipindi says:

        sorry , the question is incomplete.i have 3 more fbx files viz stand-to-sit , sit-to-type and sit-to-stand.when i press space bar stand-to-sit and sit-to-type animations should be enabled after 30 sec sit-to-stand animation should be enabled.i am using animator controller as follows…
        idle———>walkfwd
        idle———>backwd
        idle———>turnright
        idle———>turnleft
        for these animations to occur i am using w,s,d,and a keys respectively.now when i press spacebar on the keyboard stand-to-sit, sit-to-type animatios should occur continuously and after 30 sec sit-to-stand animation should be enabled.could u guide how to do this?do i need to drag and drop these 3 fbx files in the animtor controller as walkfwd ,backwd , turnright , turnleft fbx files are drag and dropped , and i have used float parameters for moving fwd , bwd ,right and left viz front (and the condition is front>0.1) ,back (and the condition is back0.1) , left (condition is left <-0.1).hope u have understood it, pls guide me

        • I’m fairly certain we covered this before. Nonetheless the answer is still the same, it’s going to come down to creating the actual animation clips. So for the sitting animations you’ll need to create those then just loop the animation of him sitting. You can either make it 30 seconds long, or use a script to determine the time (which is better in my opinion).

          • sridevipindi says:

            i am sorry …i have dowloaded from asset store pre made fbx files viz stand-to-sit , sit-to-type and sit-to-stand.i haven’t created these 3 animations…
            I am using the following script.pls correct me where i am wrong
            var vert:float;
            var horz:float;
            function Update () {
            vert = Input.GetAxis (“Vertical”);
            horz = Input.GetAxis (“Horizontal”);
            //left = Input.GetKey (“D”);

            anim.SetFloat (“Fwd”, vert);
            anim.SetFloat (“Bwd”, vert);
            anim.SetFloat (“Left” , horz);
            anim.SetFloat (“Right” , horz);

            if(Input.GetKeyDown(KeyCode.Space))
            {
            animation.play(“stand-to-sit”);
            animation.play(“sit_to_type”);
            animation.play(“sit_to_stand”);

            }
            }
            i am getting error that animation.play is obsolete……is this code correct?i am using unity 5.1.1f1 version

          • Why would you use animation.play if you’re using Mecanim? You’d use the same system to control the animations as well. For your animations just use the same function, but create parameters for these animations. So you can create one int parameter called “sit”. Then for each animation transition just increment the int parameter by one within the conditions section. So “stand to sit” would have the condition of “sit” set to 1, then “sit to type” would be 2 and so on. That way you can use one parameter and just increase the int value for different animations.

            Within your script you’d then have something like “anim.SetInteger(“sit”, 0)” or “anim.SetInteger(“sit”, 1)” and so on.

  5. sridevipindi says:

    thank u Jonathan.

  6. sridevipindi says:

    thank u

  7. sridevipindi says:

    hi jonathan
    i have to rotate an object in the game view like how we do in the scene view. i mean in the scene view we select one object and click on rotate tool , then we get 3 circles for 3 directions ie x,y and z.Then we can rotate the object by dragging on any one of the circles.in the same way, when i select an object , automatically 4 arrows(i have created 4 arrows in max and exported to unity) should be displayed so that i can rotate on all the 4 directions.could u help me pls.

  8. Hi Jonathan
    Assets/Gizmo/Gizmo.js (23,18):BCE)0017 :the best overload for the method GizmoHandle.setType(GizmoType) is not compatible with the argument list (unityEditor.GizmoType)
    some one has suggested to replace GizmoType with GizmoMyType….but no luck;pls guide me in solving this error

Trackbacks

  1. […] 3.Mecanim Tips: Basic Walk Scripting […]

Speak Your Mind

*