# Help with rotation on Z-Axis using EulerAngles

Hi, Can someone help with eulerAngles and Quaternions? I made a character moving either left or right continuously using AddForce. When a collision happen, I want it to stop and tilt upwards around 30 degrees from where the player is facing (i.e. a car that crashed and tilt at 30 deg facing the sky from the ground). In this code It works when been hit and facing RIGHT.

``````if (isHit == true)
{
speed = 0f;                                             // Player stop moving

if (playerLocalScale.x == 1)                            // Facing right
{
tilt = new Vector3(0, 0, degreesTilt);
transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, tilt, Time.deltaTime);

} else { // Facing left
tilt = new Vector3(0, 0, -degreesTilt);
transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, tilt, Time.deltaTime);
}
``````

But when the player is facing LEFT, the rotation makes a full 360 deg to its designation and it just repeats the rotation infinitely but smoothly because of Lerp. I assume it will work if I put a negative value on the z vale of tilt. but it doesn’t work. I also tried putting “-tilt” but still the same. It just rotates in a full 360 degrees. Any help and feedback is appreciated. Thanks

For a start you dont set eulerAngles directly, you set the rotation using a quaternion.

So you would want:

``````if (playerLocalScale.x == 1)                            // Facing right
{
tilt = new Vector3(0, 0, degreesTilt);
transform.rotation = Quaternion.Euler(Vector3.Lerp(transform.rotation.eulerAngles, tilt, Time.deltaTime));
} else { // Facing left
tilt = new Vector3(0, 0, -degreesTilt);
transform.rotation = Quaternion.Euler(Vector3.Lerp(transform.rotation.eulerAngles, tilt, Time.deltaTime));
}
``````

That may or may not solve your issue, as quaternions solve a lot of rotation based issues. Try that first and then report back with what happens and will provide more advice based on that.

I used this, when facing Right it works but when facing Left it still not work, it still makes a 360 deg full rotation on Z axis, I think it’s not accepting the negative value rotation. Maybe this additional details on my game will help?

It’s a 2D game where the player’s sprite is just flipping using a click. I’m not really changing the “forward” of my player (So on runtime, the red arrow is still pointing on the positive X regardless the player is facing/moving left or right). I just move on a negative X when moving Left with a facing left sprite, and a positive X when moving right with a flipped sprite facing right. Should I include a rotation of 180 and -180 deg. on Y while flipping direction?

EDIT: tried to include a 180 and -180 deg rotation. Doesn’t work that way I guess.

Hi

It seems that this code:

``````if (playerLocalScale.x == -1)                           // Facing Left
{
transform.rotation  = Quaternion.Euler(0,0, -degreesTilt);
}
``````

produces the result that I wanted. I removed the Lerp function and it now works when facing on the LEFT. though no lerp is happening ofc. How can I simulate this inside a lerp? Any help is appreciated.

You can use Mathf.Lerp on the float value itself - create a second float, lerp it towards degreesTilt, and use that in your Euler call.

If you’d like to know more about Euler angles (and why 95% of the time they’re terrible), I wrote an article about that. (The usage in your most recent code is one of the few times it’s fine to use Euler angles.)

Can you post a sample code? I’m kinda lost right now sorry, just started coding last month. Not really familiar with Mathf.Lerp. Should i put the Lerp on the float z of the Euler?

EDIT: I don’t know but for some reason its not working:

``````transform.rotation = Quaternion.Euler(0, 0, Mathf.Lerp(0f, -degreesTilt, Time.deltaTime));
``````

Your “0f” needs to be a stored value that changes slowly.

``````float currentDegrees = 0f;
void Update () {
....
currentDegrees = Mathf.Lerp(currentDegrees, -degreesTilt, Time.deltaTime);
transform.rotation = Quaternion.Euler(0,0,currentDegrees);
``````

You may also want to consider using Mathf.MoveTowards instead, which will produce a more consistent movement rate than Lerp when used in this way.

OMG thank you so much! That lerp assigning, it works perfectly now! oh the MoveTowards doesn’t work btw, maybe it’s not designed on rotations? Anyway I’ve learned so much on this single thread, thank you also to GameDevCoup for the starter code fix. You guys are awesome. Cheers

EDIT: I realized that MoveTowards uses different parameters. I might stick to Lerp for now.