How can i rotate a camera on its x or z axis using a Quaternion

Hello guys i would like to ask how can i make may camera rotate 60 degrees on its x axis when i press the Left Arrow key on the keyboard. I would be thankful if someone could help me out thank you very much in advance :slight_smile:

Also one important thing how do i make it so that when i let go of the key the camera returns back to its normal position here is a script i did for gun movement but it did not work out when i tried the same for the camera rotation :slight_smile:

		public var MoveAmount : float = 1;
		
				public var MoveSpeed : float = 2;
				
						public  var GUN: GameObject;
							public  var CAM: GameObject;
						
						
							public var MoveOnX : float;
							
							  public var MoveOnY: float;
							  
							  		public var DefaultPos : Vector3;
							  		
							  		public var NewGunPos : Vector3;
							  		
							  		public var ONOff : boolean = false;
							  		
							  		
		
function Awake(){


  DefaultPos = transform.localPosition;
  ONOff = true;
 
}



function LateUpdate () {


   if(ONOff == true){
     
			MoveOnX = Input.GetAxis( "Mouse X") * Time.deltaTime * MoveAmount;
			MoveOnY = Input.GetAxis( "Mouse Y") * Time.deltaTime * MoveAmount;
			NewGunPos = new Vector3 ( DefaultPos.x+ MoveOnX, DefaultPos.y + MoveOnY, DefaultPos.z);
			GUN.transform.localPosition = Vector3.Lerp(GUN.transform.localPosition , NewGunPos, MoveSpeed * Time.deltaTime);
			

}

else{

   ONOff = false;
   
 GUN.transform.localPosition = Vector3.Lerp(GUN.transform.localPosition, DefaultPos , MoveSpeed *Time.deltaTime);


}

}

That’s a good job for a coroutine. Call the coroutine RotateCam when the key is pressed, and it will do the full cycle: rotate 60 degrees around X in duration seconds, wait until key released, then return to the initial position in the same time.

var duration: float = 1.2; // duration in seconds
private var orig: Quaternion;
private var dest: Quaternion;
private var rotateCamFlag = false;

function RotateCam(){
    if (rotateCamFlag) return; // aborts if already rotating
    rotateCamFlag = true;      // flag indicates it's rotating
    var start = transform.rotation; // save the start rotation...
    var dest = Quaternion.Euler(60, 0, 0) * start; // and calculate the destination
    var t: float = 0;
    while (t <= 1){
        t += Time.deltaTime/duration; // advance t a little step
        transform.rotation = Quaternion.Slerp(start, dest, t); // rotate this little
        yield; // return, but resume here next frame
    }
    while (Input.GetKey("left")){ // wait while key pressed...
        yield; // but returning each frame to not stop Unity
    }
    while (t >= 0){
        t -= Time.deltaTime/duration; // t go back to 0 a little step
        transform.rotation = Quaternion.Slerp(start, dest, t); // rotate this little
        yield; // free Unity until next frame
    }
    rotateCamFlag = false; // rotation ended
}

To do the magic, just call RotateCam when left arrow pressed:

  if (Input.GetKeyDown("left")) RotateCam();

NOTE: This function always rotate 60 degrees, wait key up and rotates back, even if you release the key too soon.

EDITED: I tested the script above and it worked fine, but you can have the same effect without coroutines with the script below (values adapted to match the video that you linked):

private var duration: float = 0.6; // time in seconds
private var orig: Quaternion;
private var dest: Quaternion;
private var t: float = 0;

function Update(){
	// t > 0 when cycle running; avoid new cycle when t > 0
	if (Input.GetKeyDown("left") && t <= 0){ 
		orig = transform.localRotation;
		dest = Quaternion.Euler(-30, 0, 0) * orig;
		t = 0;
	}
	if (Input.GetKey("left")){
		t += Time.deltaTime/duration;
	}
	else {
		t -= Time.deltaTime/duration;
	}
	t = Mathf.Clamp(t, 0, 1);
	transform.localRotation = Quaternion.Slerp(orig, dest, t);
}

EDITED2: Well, this is a 3rd version - this time using Mouse X and returning to 0 rotation when the mouse stops. It seems that that’s what your script does, but in this version the rotation can be changed from left to turn right and vice versa if the mouse is moved.

var duration: float = 0.2; // time in seconds
var angle: float = 12; // angle to turn left or right
var limit: float = 0.3; // set the sensitivity
var speed : float = 100; // isn't used in this code

private var orig: Quaternion;
private var dest: Quaternion;
private var dir: float;

function Start(){
  orig = transform.localRotation;
}

function Update(){
  // filter the mouse input with Lerp
  dir = Mathf.Lerp(dir, Input.GetAxis("Mouse X"), 5*Time.deltaTime);
  dest = orig; // define the dest rotation according to the dir variable
  if (dir > limit) dest *= Quaternion.Euler(0, 0, -angle);
  if (dir < -limit) dest *= Quaternion.Euler(0, 0, angle);
  transform.localRotation = Quaternion.RotateTowards(transform.localRotation, dest, angle*Time.deltaTime/duration);
}