Quaternion.Slerp still rotating object instantly (rather than over a number of seconds)

Hi - I’m struggling to get my camera to return to 0,0,0 over a number of seconds, as at the moment it transforms instantly.

Does anyone know what bit I’m doing wrong?

var mouseIcon : GameObject;
var mainCam : GameObject;
var speed : float = 10;



function Start () {

mainCam.GetComponent(MouseLook_unbugged).enabled = false;


}
 
 function Update()
 {
 
 if(mouseIcon.activeInHierarchy == true) {
 

 mainCam.GetComponent(MouseLook_unbugged).enabled = true;

 
 
 }
 
if(mouseIcon.activeInHierarchy == false) {

 mainCam.GetComponent(MouseLook_unbugged).enabled = false;
 

  mainCam.transform.rotation = Quaternion.Slerp(Quaternion.Euler(0, transform.eulerAngles.y, 0), Quaternion.Euler(0,0,0), speed * Time.deltaTime);

 
 


}

}

All the best, Laurien

This line has potential problems:

     mainCam.transform.rotation = Quaternion.Slerp(Quaternion.Euler(0, transform.eulerAngles.y, 0), Quaternion.Euler(0,0,0), speed * Time.deltaTime);

The issue is that, unless your object is only being rotated on the ‘y’ axis, then reading from eulerAngles.y is problematic. There are multiple rotations that represent any given physical rotation, and Unity will change the representation on you. So even if you object appears to be rotated on the ‘Y’, you may get 0.0 for that rotation causing an immediate change to your destination rotation.

To start with, change the line to:

   mainCam.transform.rotation = Quaternion.Slerp(mainCam.transform.rotation, Quaternion.identity, speed * Time.deltaTime);

If this rotates over time, but is not the rotation you are looking for, then you need to describe the rotation. That is this is not a Slerp() issue, but an issue with you getting the rotation you need for your object.

P.S. I noticed that you are using ‘transform.eulerAngles.y’, not ‘mainCam.transform.eulerAngles.y’ in your rotation. This may also play a role in your problem.