Animations are not playing with certain directions?
Hello everyone! So I'm trying to get four-way movement with a 3D player character working with both keyboard and controller. The player's Animator is using a 1D blend (0 = idle, 0.5 & -0.5 = walk,). The issue is that for some reason my Walk right (0.5) and Walk Left(-0.5) animations will play when the Horizontal Axis on the keyboard or controller are used. BUT The walking animation doesn't play when my character is moving forward or back. I'm sure I'm making some sort of mistake I just don't know what.
Side note: My character continues moving(sliding) after I've stopped walking. This only happens when using the keyboard and not with the controller. I'm not sure if this is related to the above problem or not.
//Gets Attached Players Animator
playerAnimator = playerObject.GetComponent<Animator>();
//Forward/Left-Right Param
var inputy = playerAnimator.GetFloat("InputY");
//Movment Controller
if (Input.GetAxis("Vertical") >0)
{
transform.position += Vector3.forward * Time.deltaTime * moveSpeed;
playerAnimator.SetFloat("InputY", 0.5f);
}
if (Input.GetAxis("Vertical") < 0)
{
transform.position += Vector3.back * Time.deltaTime * moveSpeed;
playerAnimator.SetFloat("InputY", -0.5f);
}
if (Input.GetAxis("Vertical") == 0)
{
transform.position += Vector3.zero * Time.deltaTime * moveSpeed;
playerAnimator.SetFloat("InputY", 0);
}
if (Input.GetAxis("Horizontal") > 0)
{
transform.position += Vector3.right * Time.deltaTime * moveSpeed;
playerAnimator.SetFloat("InputY", 0.5f);
}
if (Input.GetAxis("Horizontal") < 0)
{
transform.position += Vector3.left * Time.deltaTime * moveSpeed;
playerAnimator.SetFloat("InputY", -0.5f);
}
if (Input.GetAxis("Horizontal") == 0)
{
transform.position += Vector3.zero * Time.deltaTime * moveSpeed;
playerAnimator.SetFloat("InputY", 0);
}
Answer by jchester07 · Nov 11, 2017 at 09:04 AM
Why are you setting it to 0.5f? Are you reserving the value of 1 for other things?
Basically, Axis are just between -1 to 1. You can easily set the values like this
float horizontalMovement = Input.GetAxis("Horizontal");
// if you want to keep it at 0.5, do this.
// horizontalMovement /= 2f;
transform.position += Vector.right * horizontalMovement * Time.deltaTime * moveSpeed;
playerAnimator.SetFloat("InputY",horizontalMovement);
Obviously, forward and back animation won't work because you set the same float (InputY) at the same value of 0.5f/-0.5f which is also the same that of the horizontal(left/right).
It's better that you should use 2D blend in this cases.