Hello,
I’m learning the basic of animations. My script is this below; But the issue i’m having is my char is walking when “KeyCode.W” is pressed down. But he isn’t going forward. He’s just in 1 place.
using System.Collections;
using UnityEngine;
public class Controller : MonoBehaviour {
private Animator animComp;
private int movementState;
private object runMove;
private object playerModel;
private Vector3 forceFactor;
private readonly object direction;
private enum CharacterMovement {Idle = 0, Walk = 1, Running = 2 };
// Use this for initialization
void Start ()
{
animComp = this.GetComponent();
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.W))
{
movementState = (int)CharacterMovement.Walk;
}
else if (Input.GetKeyUp(KeyCode.W))
{
movementState = (int)CharacterMovement.Idle;
}
//ajAvatar is my Parameters.
animComp.SetInteger(“ajAvatar”, movementState);
}
}
That should work if your animator is set up correctly. The main things you have to check:
-
The animator controller should have an int parameter called “ajAvatar” and it should be used in a BlendTree or something to decide between walk/run/idle animations.
-
The actual animations used for Walk and Run need to have “Root Motion” enabled, and they need to actually have root motion in them. You can look at the animation preview in the inspector. If it’s just an animation of someone walking or running in place, then it doesn’t have root motion and it’s not going to work. It needs to be an animation where they actually move forward when walking or running. There are some free Unity animations in the Standard Assets package of a guy walking and running forward with full root motion that should work. If you really want to use the walking-in-place animations, then you’ll need to also use the Unity Character Controller or do some scripting of your own to determine how fast the player should move when they’re pressing the buttons.