How to reset object's rotation once on a certain position?

Hello, I was trying to make a method in which a rotation is added to the player until it reaches 0 in the x axis. Here’s what I got so far:

``````    void RotateToCenter()
{
if (junctionOn)
{
if (player.transform.position.x < -0.01)
{
player.transform.rotation = Quaternion.Euler(0, 0, -45);
}
if (player.transform.position.x > 0.01)
{
player.transform.rotation = Quaternion.Euler(0, 0, 45);
}
if(Mathf.Approximately(player.transform.position.x, 0))
{
player.transform.rotation = Quaternion.identity;
}
}
}
``````

The problem is, once it goes to the middle, the player keeps jittering and shaking. How do I make it so that it resets the rotation once on the 0 without weird behavior? Thanks.

Hi there, I’m going to guess it’s pinging between different cases of your if statement very quickly, causing the jitter effec you describe. I suggest using an else if and else condition, as well as specifying the zero rotation more explicitly as Quaternion.Euler(0,0,0).

`````` void RotateToCenter()
{
if (junctionOn)
{
if (player.transform.position.x < -0.01)
{
player.transform.rotation = Quaternion.Euler(0, 0, -45);
}
else if (player.transform.position.x > 0.01)
{
player.transform.rotation = Quaternion.Euler(0, 0, 45);
}
else
{
player.transform.rotation = Quaternion.Euler(0,0,0);
}
}
}
``````

Let me know if this helps!