I have a character that I move with WASD and rotate direction with mouse. I’m trying to determine which direction the character is moving (ex, forward, backward, left, right) based on it’s current and previous position vectors. I tried this code:
Vector3 direction = (gameObject.transform.position - previousPosition).normalized;
if (direction.z == 1)
{
//forward
AnimateWalk(true, false);
}
if (direction.z == -1)
{
//backwards
AnimateWalk(false, true);
}
if (direction.z == 0)
{
//no forward no back
AnimateWalk(false, false);
}
if (direction.x == 1)
{
//right
}
if (direction.x == -1)
{
//left
}
if (direction.x == 0)
{
//no left right
}
previousPosition = gameObject.transform.position;
It works great if I walk in a straight line but if I rotate the character the normalized value will changed based on the rotation and so I need to take the rotation into account some how.
I need something that will give me results something along the lines of:
Forward = 1
Backward = -1
No change = 0
Left = -1
Right = 1
No change = 0
I’ve tried various methods suggested on the forums and here but had no luck in producing something that will work. The main reason why I want to play the animation this way is because this is a multiplayer game with an authoritative server and instead of sending an RPC every time a new player direction animation is to be played I can have the client determine the proper animation from the gameObjects last and current position vectors.
EDIT: If it helps the character is being moved using the transform.forward and transform.right vectors.