Tilt an object and level it back out.

Hello, What I’m trying to achieve with my player controls (in a flying game) is to tilt the player side to side using mouse controls. I have done that successfully but my hang up is leveling the player back out. I need it to level out constantly and not just when the mouse has no input, it takes constant movement to tilt.

I have already prototype this out in blender using python. Tilting was simply done by assigning the rotation value to an axis and then aligning the up axis to a vector:

player.alignAxisToVect([0.0,0.0,1.0], 2, 0.1)

that is what axis to align, which vector, and over how much time.

In the following code written in C# “tiltPoint” refers to an object that rotates with the player rig but does not rotate on the z-axis and “mainP” is the main parent of the rig which all the other movement is applied to (and what I want to align with, no need to restrict any axis). I’m using a Quaternion Slerp for rotation. As written it tilts the player just fine but still doesn’t level out, I don’t understand why.

yRotation += Input.GetAxis("Mouse X");

//tilt the player
		float oldTiltX = tiltPoint.transform.eulerAngles.x;
		float oldTilty = tiltPoint.transform.eulerAngles.y;
		
		player.transform.rotation = Quaternion.Slerp(Quaternion.Euler(new Vector3(oldTiltX, oldTilty, yRotation * 5)),     mainP.transform.rotation, Time.deltaTime * 6);

As I understand it “(Quaternion.Euler(new Vector3(oldTiltX, oldTilty, yRotation * 5)” should be my from and “mainP.transform.rotation” should be my to. Yet it never levels out but tilts just fine. Is there a better way to do this or am I making a simple mistake?

Any help is appreciated!

If I understood you correctly can’t you simply have yRotation ease back to zero at the end of the frame?

yRotation -= Mathf.Sign(yRotation) * Time.deltaTime * LEVEL_OUT_AMOUNT;

Im more of a fan of rotatetowards… this will rotate from whatever to zero’s (as an example)

player.transform.rotation = Quaternion.RotateTowards(player.transform.rotation, Quarternion.Euler(0,0,0), 1);

Solved!
Lukas_GMC, you understood perfectly and where my logic failed you excelled. @JamesLeeNZ your suggestion seems like it should have worked and it’s essentially what I was already trying (which I also thought should work). Maybe there’s some problem because I’m already directly changing the rotation and doing it again doesn’t register… For curiosities sake I’d like to know why it doesn’t work. Because I’m already manipulating the rotation with my var “yRotation” it would make perfect sense to edit that value directly rather than try to alter the transform in two different ways.

Thank you for the help! My player controls are now completed and I can move onto more complicated things.