x


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.

more ▼

asked Dec 22 '09 at 05:44 PM

cry0 gravatar image

cry0
40 6 6 9

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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

more ▼

answered Dec 22 '09 at 06:46 PM

Ashkan_gc gravatar image

Ashkan_gc
9.1k 33 56 117

Many thanks. Tom also answered this question on the forum and I have posted there an updated version of the code: http://forum.unity3d.com/viewtopic.php?p=248370&highlight=#248370

Jan 08 '10 at 01:46 PM cry0
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Dec 22 '09 at 07:31 PM

Peter Alexander gravatar image

Peter Alexander
931 5 6 23

Many thanks for answering.

Jan 08 '10 at 01:47 PM cry0
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5070
x2995

asked: Dec 22 '09 at 05:44 PM

Seen: 1043 times

Last Updated: Dec 22 '09 at 05:44 PM