lerp to 0, problem with horizontal value

Hi,

I would like my character to go back to its original z rotation (straight) when I do not use the keyboard arrows anymore. But I think that, because there is still a value in the horizontal axis, the character continues to lean on the right or the left depending on which key i press. And then, when the value is at 0, it goes back to its rotation by default.

Would you know how to avoid this short time lapse and directly ask the character to go back to the original rotation in z?

Here is the code :

function Update () {
	//rotate z keyboard
	Debug.Log("horiz : "+Input.GetAxis("Horizontal"));
	
	if ( Input.GetAxis("Horizontal") ){ /*  leaning  */
		
		transform.Rotate( -Vector3.forward * 100 * Time.deltaTime * Input.GetAxis("Horizontal") );
		transform.eulerAngles.z = ClampAngle(transform.eulerAngles.z, 10);
		
	} else {
		var m_lerp; /*  returns to z rotation = 0 */
		if (transform.eulerAngles.z < 180)
			m_lerp = Mathf.Lerp(transform.eulerAngles.z, 0, 10 * Time.deltaTime);
		else 
			m_lerp = Mathf.Lerp(transform.eulerAngles.z, 360, 10 * Time.deltaTime);
			
		transform.eulerAngles.z = m_lerp;
	}

Thanks

Input.GetAxis returns a float, not a boolean - you should compare it to some limit, like this:

if (Mathf.Abs(Input.GetAxis("Horizontal")) > 0.01 ){ /*  leaning  */
    ...

This way anything out of the range -0.01…+0.01 will be considered “no key” condition. You can increase the limit if a faster response is desired - try 0.1, for instance.