So this is something that’s been bothering me probably the most out of all my Unity problems I’ve encountered throughout my game developing experience. Mainly because of how simple it should be, yet it’s so hard for me to figure out.
I do currently have something that sort of works, and I’m not sure if it will cause any issues later on, so I want to eliminate it.
if (Input.GetKeyDown (KeyCode.A)) {
anim.SetBool ("Run", true);
transform.localScale = new Vector3(-1f, 1, 1);
anim.Play ("Walk");
}
else {
anim.SetBool ("Run", false);
}
if ((Input.GetKeyDown (KeyCode.D))) {
anim.SetBool ("Run", true);
transform.localScale = new Vector3(1f, 1, 1);
}
else {
anim.SetBool ("Run", false);
}
This is what i have so far. The “Run” parameter is set to true when the player presses either the A or D key. And set to false when they aren’t pressing it. Therefore playing and stopping the “Walk” animation I have set up.
The problem here is that for flipping, I’m changing the local scale of the character’s sprite, and for some reason flipping it to the left, causes the animation to not play, so I have to make it play manually by calling it in the code. This is what i want to avoid, because I don’t know if it’s going to cause any problems.
I recently watched another tutorial, and this is what I got from it.
transform.localEulerAngles = new Vector3(0, 180, 0);
I used this instead of the first localScale line. The problem with this one, is that when I go left, the animation doesn’t play, and since the whole object is being span 180 degrees, the character moves to the right rather than to the left.
Could someone please tell me the easiest, and most up-to-date method of flipping the sprite, while still allowing for the animation to play when it’s flipped (without having to play it manually like I have done)?