Hello everyone,
I am currently working in a top down view game and I am stuck with the following.
I managed to get the player rotate towards the direction it’s going, however when there is no input, the player resets back to the original rotation. I want to be able to make the player to face the last direction it was going. (I don’t know if I made myself clear). Say the default rotation is towards the right, so if I go up and then release the input, the player will reset its rotation towards the right instead of staying towards the up.
Thank you.
Thank you for your help, I do understand what you are saying about transVector being set based on speed, but I don’t quite understand how to use Vector3.MoveTowards. I will read on it and see what I can find.
Its because of the way you calculate and rotate towards your angle.
If you have 0 horizontal and 0 vertical, your target direction is (0,0,0). I forget trig, but it looks like youre doing a calculation been current postion to target position to determine what angle your character should be facing, then you start rotating towards that direction. Obviously if that target is Vector3.zero, its gonna rotate that direction…
easiest fix would be to not rotate if horizontal and vertical are both 0.
float vertical = Input.GetAxisRaw("Vertical");
float horizontal = Input.GetAxisRaw("Horizontal");
if(Mathf.Abs(vertical) > 0 && Mathf.Abs(horizontal) > 0)
{
//do all your movement & rotation code
}
else
{
anim.SetFloat("speed", 0);
}
Thank you very much! This did the trick, however I think that you meant
to say
if(Mathf.Abs(vertical) > 0 || Mathf.Abs(horizontal) > 0)
{
//do all your movement & rotation code
}
Because when I put what you said, the movement would be tied to pressing two keys at the same time, then I changed the and for or and then it all worked out. Thank you very much!