need help with centering camera - script included

Hi everyone,

I have tried to make a script to attach to the main camera so that whenever a user is idle for X time, the camera rotates back to it's original position.

The thing is that I am not able to do it permanently.

Follows my code:

var origem ;
var speed = 0.5;
var reset = 0.0;
var maxReset = 5.0;

function Start(){
   origem = transform.rotation;
}

function dostuff (){
   transform.rotation =
      Quaternion.Slerp (transform.rotation, origem, Time.time * speed);      
}

function OnGUI () {

   var event = Event.current;

   if (!event.isKey && !event.isMouse && event.type != EventType.MouseMove){
      reset += Time.deltaTime;
   }   

   else{
      reset = 0.0;
   }

   if (reset > maxReset){
      dostuff ();
      reset = 0.0;
   }
}

Any suggestions?

Many thanks.

slerp function needs to run multiple times to rotates the object in time by the speed that you specified. you just run the function one time in each time that reset is greater than maxreset. create a coroutine that executes till rotattion of your gameobject is equal to the second parameter of your slerp function or returned value has zero in all x,y,z,w values

You should be able to fix the code you've given simply by removing the `reset = 0.0;` just after the call to `dostuff()`, so that the slerp repeats so long as the player is inactive.

It's also worth noting that you should move this logic out of OnGUI and into Update as OnGUI is called at irregular intervals. If you move it, you'll also need to stop using Events and use Input.GetKeyDown, Input.GetMouseButtonDown etc.