0
\$\begingroup\$

I am trying to make a game where the player can only rotate to 4 angles. Something like this

I cannot use Quaternion.Rotatetowards because the player's position is not constant, but I don't have the problem with movement. The problem I am having is that when I use transform.Rotate of 90 degrees it creates a sudden rotation. I would like to have that rotation smooth. I have tried using Slerp but I don't think I quite get the syntax I need to use.

Both Vector3.Slerp, and Quaternion.Slerp interpolate between two different positions, when my player turns I would like him to keep his current position, and only turn his "face" towards the desired position. I would appreciate the help. Thanks.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You simply lerp between these angles.

Here is a part of my third person character controller script, that smoothly rotates the model in the direction that it's moving:

Vector3 angle = Vector3.zero;
if(moveDirection.x != 0)
{
    if(moveDirection.z == 0)
    {
             if(moveDirection.x > 0) angle.y =  90;
        else if(moveDirection.x < 0) angle.y = -90;
    }
    if(moveDirection.z > 0)
    {
             if(moveDirection.x > 0) angle.y =  45;
        else if(moveDirection.x < 0) angle.y = -45;
    }
    if(moveDirection.z < 0)
    {
             if(moveDirection.x > 0) angle.y =  135;
        else if(moveDirection.x < 0) angle.y = -135;
    }
}
else if(moveDirection.z < 0) angle.y = 180;
angle.y = Mathf.LerpAngle(character.transform.localEulerAngles.y, angle.y, 10 * Time.deltaTime);
character.transform.localEulerAngles = angle;

This is for any of 8 possible direction, based on ASDW keys, with x axis being AD and z axis being SW. Using angle.y rotates the object on a flat 3d surface. For a 2d game you should rotate the z axis of your object.
You can make it work for 4 angles only if you remove conditions for +-45/135 degrees.

But if you want to lerp between one of 4 random angles you could make an array of angles and use LerpAngle to switch between them.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .