How to make the camera return to unitial position when no button are pushed

Hello i’m looking for a way to make the camera rotation back to the initial position when no button are pushed on the joystick. Right now my code is :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MouseLook : MonoBehaviour

{

public float mouseSensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;

// Start is called before the first frame update
void Start()
{
    Cursor.lockState = CursorLockMode.Locked;
}

// Update is called once per frame
void Update()
{
    
float mouseX = Input.GetAxis("Horizontal") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Vertical") * mouseSensitivity * Time.deltaTime;

xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90);

transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);

}

}

i’m very much a beginner, need some help please !

Simplest would probably be to do sth like this:

if(mouseY == 0.0f){
    xRotation = 0.0f;
}else{
    ...
}

but that would snap your rotation instantly. I suggest to read about linear interpolation to smooth it out (Mathf.Lerp). With lerping you’d do sth like:

if(mouseY == 0.0f){
    xRotation = Mathf.Lerp(xRotation, 0.0f, 0.1f);
}else{
    ...
}

Where 0.1f is your step value, lower it to make it slower, or vice versa.
You can also make it just like your original approach, so lower value towards 0.0f by increasing/decreasing it. But give lerp a try, it makes the transition much smoother.

Thanks for the answer @Shrimpey ! It doesn’t seems to work. I think when i use
if(mouseY == 0.0f){ …
the camera go back the its original position only when the camera is already at its original position 0. I think i need a code that say if mouseY doesn’t have any acceleration… I don’t know if i’m clear