I’m currently trying to build a script to handle the players movement for a 2d top-down rpg. The animation is fine for all directions, as well as moving idle.
The problem I’m having is when holding the left arrow to move left, then switching to down, the character will start to move down, but the animation is still running to the left for half a second to a second. This only happens generally when moving left or right, and attempting to then go north and south.
For a simplified script :
public class playerController : MonoBehaviour
{
private Animator animator;
// Use this for initialization
void Start()
{
animator = this.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
var vertical = Input.GetAxis("Vertical");
var horizontal = Input.GetAxis("Horizontal");
if (vertical > 0)
{
animator.SetInteger("Direction", 2);
}
else if (vertical < 0)
{
animator.SetInteger("Direction", 0);
}
else if (horizontal > 0)
{
animator.SetInteger("Direction", 1);
}
else if (horizontal < 0)
{
animator.SetInteger("Direction", 3);
}
}
}
Basically, is there any way to force an animation switch? I tried speeding up the animation, which did not correct the issue. Also, I tried to lower the time in the transition, which had no effect. To give an overall idea, if I hold up for a second, then left for a second, and keep going back and forth, the player will never face left, but will continue to move up then left, etc. Thanks!
Note: This is for a Unity 2D project from 4.3, with a Fire Emblem, GBA Zelda type view.
Why just the down vote with no explanation of why? Would it have been better if I didn't post the answer to my problem?
– Oddeye01I think the answer you've posted, while it may work, isn't a "Best Practice" solution. If it's just a switch, I'd suggest always having the character moving "forward", and simply change the rotation in code.
– infinitypbrInteresting, I've read your answer but cant see why using hard code switches is better then the animator? Whats the problem with one vs the other. And why is code switches best practice? The 2d example by unity uses the animator approach albeit without the need of vertical movement, thus avoiding gravity. Thanks! :D
– Oddeye01Personally, having to adjust the gravity for this seems to be a difficult way to solve the problem. I'm imaging the original zelda, top-down, but that you're doing it in a 3d world, correct? I suppose that's what i got from "Top Down 2D", since Unity's 2D stuff is made for side-scrolling. If I'm wrong, then I guess adjusting gravity would be your solution. But if it's top-down (like literally camera is pointing down) in a 3D world, then just adjusting the direction the character is facing and always moving "forward" seems far more elegant.
– infinitypbr