Hi, so I have a character I made in Maya and imported to Unity with animations and all. I'm just learning Mecanim and doing some testing. I have my character moving and am trying to understand it. However, I wanted him always facing the direction he's going, which he wasn't. On Unity Answers, I found this handy piece of code that makes my character always face the direction the player presses.
transform.forward = Vector3.Normalize(new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"))); // Normalizes the rotation of Z local while moving
So now while he faces the direction of whatever direction I press, he only animates when I press forward. If I press any other direction, he moves in that direction, but the animation stops. Also, if the player is no longer pressing that direction, the model instantly snaps to his native Z direction. To be honest, I don't quite understand Mecanim completely just yet, though I've been doing a Digital Tutors tutorial on the subject to gain some understanding. Even so, while I am understanding the gist of the code and methods, I'm having trouble pinpointing where my script accesses the walk blendtree of mecanim. To gain an understanding of my problem, I've attached a picture of some test models I made for my Unity scene with my character and I want him to move freely around the room.
I'm guessing the animation has something to do with the following section of my code. Does anyone know how to solve this and what I'm doing wrong or at least what's happening?
void Start ()
{
// initialising reference variables
anim = GetComponent<Animator>();
if(anim.layerCount ==2)
anim.SetLayerWeight(1, 1);
}
void OnAnimatorMove() //Tells Unity that root motion is handled by the script
{
if(anim)
{
Vector3 newPosition = transform.position;
newPosition.z += anim.GetFloat("speed")* meshMoveSpeed * Time.deltaTime;
newPosition.x += anim.GetFloat("direction") * meshMoveSpeed * Time.deltaTime;
transform.forward = Vector3.Normalize(new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"))); // Normalizes the rotation of Z local while moving
transform.position = newPosition;
}
}
void FixedUpdate ()
{
float h = Input.GetAxis("Horizontal"); // setup h variable as our horizontal input axis
float v = Input.GetAxis("Vertical"); // setup v variables as our vertical input axis
anim.SetFloat("speed", v); // set our animator's float parameter 'Speed' equal to the vertical input axis
anim.SetFloat("direction", h); // set our animator's float parameter 'Direction' equal to the horizontal input axis
anim.speed = animSpeed; // set the speed of our animator to the public variable 'animSpeed'
currentBaseState = anim.GetCurrentAnimatorStateInfo(0); // set our currentState variable to the current state of the Base Layer (0) of animation
Replies
Direction is at least a two component vector and you'll need to normalise that first, too.
So it's considering direction to be how much your stick is pointing horizontally, and the speed to be how much you're pushing forwards.
How exactly do I do that? Unfortunately, I'm not the best programmer so while I understand the gist of what you're saying I don't know how to translate that into code.