Using Mathf.LerpAngle to gradually rotate an objects x position

Hi, i’m trying to make an object that can rotate on the x axis back and forth 180 degrees and whilst the player is not pushing the rotate button and if it’s not level(which is a 0 value) to lerp back into a level position.

However with my code it moves back instantly to a level position, am i using lerp wrong? is there a different way i can achieve this without using a for loop?

Here’s my code

using UnityEngine;
using System.Collections;

public class PlayerInput : MonoBehaviour {
	public float moveSpeed;

	
	private float horizontal;
	private float verticle;
	private float rotate;
	private Transform startingTransform;
	
	void Awake(){	
		startingTransform = transform;
	}
	
	void Update(){
		
		//get axis values
		horizontal = Input.GetAxis ("Horizontal") * moveSpeed;	
		verticle = Input.GetAxis ("Vertical") * moveSpeed;	
		rotate = Input.GetAxis ("Vertical") * moveSpeed;	
		//make time constant
		horizontal *= Time.deltaTime;
		verticle *= Time.deltaTime;
		//translate player
		transform.Translate(horizontal,0,0,Space.World);
		transform.Translate(0,verticle,0,Space.World);
		
		//rotate character model within 180 degrees
		if(transform.rotation.x < 0.5f && transform.rotation.x > -0.5f){
			
			transform.Rotate(rotate,0,0);
		}
		//if the player is not leveled out and is not rotateing start the rotate them back.
		if(transform.rotation.x != 0.0f && Input.GetAxis ("Vertical") == 0.0f){
	
			float angle = Mathf.LerpAngle(360,startingTransform.rotation.x, 100 * Time.deltaTime);
			transform.eulerAngles =new Vector3(angle,0,0);
		
		}
		
	
	
}

Thanks for your time.

Transform.rotation is a Quaternion…a 4D construct in which the values don’t represent angles in the way you are using them here. From the reference: [Quaternions] are based on complex numbers and are not easy to understand intuitively. Thus you almost never access or modify individual Quaternion components (x,y,z,w); Instead, you do have access to Transform.eulerAngles. But eulerAngles also have their issues. Again from the reference: Do not set one of the eulerAngles axis separately (eg. eulerAngles.x = 10; ) since this will lead to drift and undesired rotations.

For any “physical” rotation, there are multiple eulerAngle rotations, so reading eulerAngles will not necessarily give you the values you expect. For example you could set your eulerAngles to (180,0,0) and immediately read the value and get (0,180,180) back.

One way to deal with eulerAngles is to think of them as write-only…keep your own Vector3 representing the rotation and assign its values each frame.