- Home /
How do I move a model with its walk animation?
I'm a little confused about getting a rigged model to move with its walk animation. First I created a walk cycle in Blender that just walks in place. Import everything into Unity just fine and set it up through Mecanim. The character walks when I press the proper input but it doesn't move. Here's the script I used:
public class Locomotion : MonoBehaviour {
Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
float move = Input.GetAxis("Vertical");
anim.SetFloat("Speed", move);
}
}
Then I figured maybe the rig actually has to move in Blender. So I do another walk cycle of the rig physically walking forward and import that. Except, the model will walk forward then rest back to the original position. Setting it to loop makes the model walk in place.
What am I missing in this process?
Anyone have any input?
I could've sworn there was an extra step you had to take in Unity to get a walk cycle working correctly and I thought it was in Unity's mecanim tutorial but I just watched it again and didn't see anything like that.
From what I know of $$anonymous$$ecanim, unless you've done something special in your Blend Tree, what you've described is normal for the first Blender/walk in place; you still have to tell the gameobject to "$$anonymous$$ove" which can be done in many different ways (rigidbody force, transform.translate, CC/move/simplemove, etc)
It might help to know exactly what you setup 'Speed' to be in your $$anonymous$$ecanim BlendTree.
Speed is a float which initiates the Walk animation when it exceeds 0.10. Which happens but the character doesn't move forward. I thought the root motion would make the model move according to its animation, unless I'm misunderstanding how that works
See if any of this helps
https://docs.unity3d.com/Documentation/$$anonymous$$anual/Root$$anonymous$$otion.html
In my limited use of $$anonymous$$ecanim so far, I've still done the moving with code rather than built-in which sounds like what you are trying to achieve with the second cycle
Alright, I looked over those settings and then the Root $$anonymous$$otion settings of the $$anonymous$$ecanim tutorial and I got it working now. Thanks for offering some help even though you're not completely familiar with the subject. Greatly appreciated
Answer by GEWLAR · Apr 08, 2014 at 01:11 PM
The Animation should move at the place that's right. To actually move the Object you have to write a script which realy changes its location (look at the standard assets for that).
...unless you are using Root $$anonymous$$otion. Unlike a normal clip where movement is via script/CC/physics etc and the clip is just a play-in-place, Root$$anonymous$$otion mecanim clips move the actual player.
Answer by Pheonix · Mar 23, 2016 at 11:00 AM
ADD a rigid body and use following script on your player game object.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
//the speed you want your player to move with.
public float speed;
void FixedUpdate ()
{
//This will allow you to move your character using unity default keys (WASD or arrow keys)
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;
}
}