Preventing 2d movement from stopping in a + direction when it should be an x.

I have an incredibly simple way to move the character. You use the arrow keys to move up, down, left, and right.
If you combine arrow keys, it moves the player diagonal up-right, up-left, down-right, down-left. So you have 8 directions.

The problem seems to be the fact that you have to be EXTREMELY precise to stop in the diagonal directions. Otherwise, even if you’re going up-right, you stop and are facing right or north. One has to specifically work their hardest to stop diagonal by unpressing both keys at the EXACT same time.

I want it to where it stops in diagonals smoothly. At the very least, to where one can have time to depress the keyboard keys without it changing directions to right or up instantly, even though you stopped at facing up-right.

Input Code

	//Get Input
	var hToMove = Input.GetAxisRaw("Horizontal");
	var vToMove = Input.GetAxisRaw("Vertical");
			
	//Move Player
	transform.Translate(Vector3.right * hToMove * hPlayerSpeed * Time.deltaTime);
	transform.Translate(Vector3.up * vToMove * vPlayerSpeed * Time.deltaTime);

Animation Code

//N
	if (hToMove == 0  vToMove > 0)
	{
	PlayerDirection = 0;
	}
	
	//E
	if (hToMove > 0  vToMove == 0)
	{
	PlayerDirection = 1;
	}
		
etc. 
etc.

Hmm… you could check the difference between positions of the last frame and the current frame to determine what direction the player was moving before he stopped, then set the direction he should be facing when he stops that way.

You sure this would work? If it updates too fast, it wouldnt really matter if I did a single check before stopping, right?

I was thinking more of maybe a way to just lower the sensitivity of the keys? I am not a slow key de-presser, so it has to update at a ridiculously fast speed for me to let go of BOTH arrow keys, and it detects the exact moment I depress one arrow key before the first. Does it really need to update this fast for keyboard keys?

Anyone have experience with 2D 8 directional movement in Unity3D, or any engine at all?

Here’s one possibility that comes to mind. You could use GetAxis() rather than GetAxisRaw(), and set the input parameters (e.g. gravity) so that there’s some damping when the keys are released. Then, instead of basing the direction on exact comparisons, divide the unit circle up into 8 ‘wedges’, and check to see in which of these wedges the vector formed from the current axis values falls. You’d probably also want to use a length threshold for the input vector so that you’re not trying to determine the direction when the magnitude is small.

Note that this is somewhat speculative (I’d have to try it out to be sure it would work).

Thx. If I decide to get rid of Raw, then I think this wuold be a good time to add in animations of turning as well. Gives the player a slowdown when switching directions, allowing for (theoretically) a smooth looking turn animation followed by continuous movement.