Need Help with Rotation

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.

Here is the code I am using

void movement()
    {
        //This will make the player object rotate towards where it's going
        float vertical = Input.GetAxisRaw("Vertical");
        float horizontal = Input.GetAxisRaw("Horizontal");
        Vector3 transVector = new Vector3(horizontal * speed, vertical * speed, 0.0f);
        myTransform.position += transVector; 
       

        desPos = transVector;
        desPos.z = 0.0f;
        myPos = myTransform.position;
        //desPos.x = desPos.x - myPos.x;
        //desPos.y = desPos.y - myPos.y;
        angle = Mathf.Atan2(desPos.y, desPos.x) * Mathf.Rad2Deg - 90;
        Vector3 rotVector = new Vector3(0,0,angle);
       
        myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.Euler(rotVector), 5.0f * Time.fixedDeltaTime); 
       
        if(vertical != 0|| horizontal != 0)
        {
            anim.SetFloat("speed", 1);
        }
        else
        {
            anim.SetFloat("speed", 0);
        }
    }

The reason why it resets is because you are setting transVector based on speed. Use Vector3.MoveTowards instead.

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);
}
1 Like

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!

yeah should have been or rather than and

you figured it out though.

1 Like