Question by
Antonc13 · Nov 15, 2019 at 12:52 AM ·
animationscripting problemspeed
Need help coding animation controller for my 3d player.
Hello people of unity, in my game I have a player who moves around on a top down 3d plane(X axis and Z axis). He has a animation controller with this in it:
void FixedUpdate()
{
pointPos();
var x = Mathf.Abs(Input.GetAxis("Horizontal"));
var y = Mathf.Abs(Input.GetAxis("Vertical"));
Animation(x, y);
}
void Animation(float x, float y)
{
if (speed > 0)
{
anim.SetFloat("XSpeed", x);
anim.SetFloat("ZSpeed", y);
}
else
{
anim.SetFloat("XSpeed", 0);
anim.SetFloat("ZSpeed", 0);
}
}
void pointPos()
{
if (Camera.main.gameObject.GetComponent<UIHandler>().inMenu == false)
{
if (isRunning == true)
{
point.transform.position = new Vector3(transform.position.x + Input.GetAxis("Horizontal"), 0, transform.position.z + Input.GetAxis("Vertical"));
}
else
{
if (Input.GetKeyUp(KeyCode.A))
{
point.transform.position = new Vector3(transform.position.x - 2, 0, point.transform.position.z);
}
if (Input.GetKeyUp(KeyCode.W))
{
point.transform.position = new Vector3(point.transform.position.x, 0, transform.position.z + 2);
}
if (Input.GetKeyUp(KeyCode.S))
{
point.transform.position = new Vector3(point.transform.position.x, 0, transform.position.z - 2);
}
if (Input.GetKeyUp(KeyCode.D))
{
point.transform.position = new Vector3(transform.position.x + 2, 0, transform.position.z);
}
}
}
}
The animations play at normal speedwhen im only engaging with one of the axis for example pressing A but when I engage both exmp... A and D the animations play's twice as fast. Ive been trying to figure it out for a few days now so any help would be great. Thanks.
Comment
Best Answer
Answer by lgarczyn · Nov 16, 2019 at 05:31 AM
This is likely a problem in your blendtree. You can however fix it just by changing these lines:
anim.SetFloat("XSpeed", x);
anim.SetFloat("ZSpeed", y);
To
anim.SetFloat("XSpeed", new Vector3(x, y).magnitude);
Your answer
![](https://koobas.hobune.stream/wayback/20220612214730im_/https://answers.unity.com/themes/thub/images/avi.jpg)