Hey guys, I’m trying to make my character move like Crash Bandicoot, so far I’ve done well with the rotation and the movement using Input.GetAxis(Horizontal) and Input.GetAxis(Vertical) so that I may play it with both keyboard and joypad. Now, my problem is when I have to move my character in diagonal directions: when I go let’s say to north-west and then i pull my finger from one of the keys to make it go only north or west, the character still goes to the diagonal direction until the Axis goes to Zero, and only then it turns around to the right direction and starts going there. That’s because I made a function that checks when I press the keys and then put a “direction” variable to the angle I’m pointing. That’s the code:
var x = Input.GetAxis("Horizontal");
var y = Input.GetAxis("Vertical");
if (y > 0 && x == 0)
{
direction= 0;
}
else if (y < 0 && x == 0)
{
direction= 180;
}
else if (x > 0 && y == 0)
{
direction= 90;
}
else if (x < 0 && y == 0)
{
direction= 270;
}
else if (y > 0 && x > 0)
{
direction= 45;
}
else if (y > 0 && x < 0)
{
direction= 270 + 45;
}
else if (y < 0 && x > 0)
{
direction= 90 + 45;
}
else if (y < 0 && x < 0)
{
direction= 180 + 45;
}
I didn’t write here the movement and the rotation code because I think that’s not the problem.
I thought one solution could be to check the previous frame and see if one of the Axis has a different value and the other it’s still the same, so that I may do something to correct this problem (like put to Zero the Axis decreasing/increasing). Is there a function that could make me do that?
Obviously that’s one solution. If someone knows a better solution, will you let me know? Thank you!