- Home /
Help With Simple Rotation
I'm struggling to get my head around this, but basically - I have a ResetRotation function in where I want it to:
yield WaitForSeconds(2);
and then reset the Y rotation to '0' over time (so it looks like its animated).
How would one go about doing that?
Thanks
Answer by leonalchemist · Jun 01, 2011 at 01:09 PM
it's probably be better if u use the animation tab in unity, there a good basic tutorial on the unity site that shows u how to do it. check here for the videos : http://unity3d.com/support/resources/tutorials/video-animation-view
or maybe what u can do is something like this
transform.Rotate(Vector3.right * Time.deltaTime);
in this case it says its rotates the object on the x axes by 1 degree per sec, so not sure how to get exactly what u want myself but its a start.
Answer by RetepTrun · Jun 01, 2011 at 05:22 PM
If you could keep track of how far you are rotating as you are doing it, then you could just rotate the same amount again but negative.
Answer by Anxo · Jun 01, 2011 at 05:32 PM
Check documentation on how to use RotateTwoards. Or you can use
var reset : boolean;
function Update(){
if(reset )
{
transform.Rotate(0,Mathf.Lerp(transform.rotation.y, 0,Time.time),0);
}
}
something like that would rotate it back to 0 on the y axis over 1 second whenever reset is true.
to activate it you could have
reSet();
function reSet(){ reset = true; yield WaitForSeconds(1); reset = false; }
Time.time starts counting at the start of the game, not at the start of this specific reset. In the reset function Anxo posted you could have var timer : float = Time.time and in the transform.Rotate replace Time.time with Time.time-timer
@Oliver Jones when you say "This does not work, any idea why? What do you mean? does it pop out an error? link the code you tried it with, $$anonymous$$e was just an example to use as base you may not be able to copy and paste.
please link the code you tried this with.
Answer by flaminghairball · Jun 07, 2011 at 01:28 PM
This is probably easiest done with a coroutine.
Typed in browser - may or may not compile ;)
function ResetRotation(){
var oldY : float = transform.eulerAngles.y;
var t : float = 0.0;//animation time
var speed : float = 1.0; //how fast you want the rotation to happen
while(t < 1.0){
transform.eulerAngles.y = Mathf.Lerp(oldY, 0.0, t); //rotate to the new position
t += Time.deltaTime * speed; //animate the time variable
yield; //yield til the next frame
}
}
Just call that function when you're ready to rotate back to 0, and it should handle the rest.