I'm new to unity and I am facing problems with the animator controller component.
When pressing on Z button, my player is supposed to start moving forward, but the animation won't start.
Here is my code:
public Animator anim;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.Z))
{
anim.SetFloat("MoveX",0.17f);
Debug.Log("hhh");
}
else
{
anim.SetFloat("MoveX", 0);
}
if (Input.GetKey(KeyCode.Q))
{
anim.SetFloat("MoveZ",0.208f);
Debug.Log("stiw");
}
else
{
anim.SetFloat("MoveZ", 0);
}
if (Input.GetKey(KeyCode.S))
{
anim.SetFloat("MoveX",-0.166f);
}
else
{
anim.SetFloat("MoveX", 0);
}
if (Input.GetKey(KeyCode.D))
{
anim.SetFloat("MoveZ",-0.2f);
}
else
{
anim.SetFloat("MoveZ", 0);
}
}
Moving backward and starting right works properly, except moving forward and moving to the left
Here is a screenshot of my blend tree
Well look at your conditions in Update
again and think what happens if you press the forward button (Q):
anim.SetFloat("MoveZ",0.208f);
anim.SetFloat("MoveZ",0);
see it now? The last two checks overrule the first two and "eat" you input;)
You should make your cases more exclusive like eg
void Update()
{
if (Input.GetKey(KeyCode.Z))
{
anim.SetFloat("MoveX",0.17f);
Debug.Log("hhh");
}
else if (Input.GetKey(KeyCode.S))
{
anim.SetFloat("MoveX",-0.166f);
}
else
{
// Only reached if neither Z nor S is pressed!
anim.SetFloat("MoveX", 0);
}
if (Input.GetKey(KeyCode.Q))
{
anim.SetFloat("MoveZ",0.208f);
Debug.Log("stiw");
}
else if (Input.GetKey(KeyCode.D))
{
anim.SetFloat("MoveZ",-0.2f);
}
else
{
// Only reached if neither Q nor D is pressed!
anim.SetFloat("MoveZ", 0);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.