- Home /
Scripting: make thing repeating
im making a door (simple door) and i want to make a rotation animation with the script...i want some thing on the door rotate and keep rotating...i also need it for other things but for now... this is my exemple
transform.Rotate(0,-900*Time.deltaTime,0);
yield WaitForSeconds(0.05);
transform.Rotate(0,-900*Time.deltaTime,0);
yield WaitForSeconds(0.05);
transform.Rotate(0,-900*Time.deltaTime,0);
yield WaitForSeconds(0.05);
transform.Rotate(0,-900*Time.deltaTime,0);
yield WaitForSeconds(0.05);
transform.Rotate(0,-900*Time.deltaTime,0);
yield WaitForSeconds(0.05);
supose that i want to repeat this 100 undread times...is there a way just write *100 and it keep repeating 100 times?
That whole snippet doesn't make any sense ;) deltaTime is used when you do something every frame to correct framerate fluctuations. Using it anywhere else, especially when it's already time based will make things even worse.
Answer by Bunny83 · Aug 12, 2012 at 01:32 AM
I guess you want to rotate your door by a certain amount of degree over a certain timespan.
Usually you would work with a target rotation you want to reach. This can be "lerped" (linearly interpolated) over time.
You can still use your approach above, but that low WaitForSeconds values might get inaccurate at lower framerates.
This would rotate the object relatively by -90°
function RotateDoor()
{
for (var i = 0; i < 100; i++)
{
transform.Rotate(0,-0.9,0);
yield WaitForSeconds(0.05);
}
}
when you call that function the door will rotate -90 in around 5 seconds. WaitForSeconds accumulated in a loop will not be very accurate in the end. Most the time it will take longer than you intended.
A better way is to do something like that:
function RotateDoor(rotate : Vector3, time : float)
{
var initialRot = transform.localRotation;
var targetRot = transform.localRotation * Quaternion.Euler(rotate);
var t = 0.0
while (t < 1.0)
{
transform.localRotation = Quaternion.Slerp(initialRot, targetRot, t);
t += Time.deltaTime / time;
yield;
}
}
This coroutine will perform any relative rotations within the given time.
Some examples:
// Rotates around y axis by -90° in 2.5 seconds
RotateDoor(Vector3(0, -90, 0), 2.5);
// Rotates around y axis by 90° in 1 seconds
RotateDoor(Vector3(0, 90, 0), 1.0);
// Rotates around x axis by 45° in half a seconds
RotateDoor(Vector3(45, 0, 0), 0.5);
Answer by Voltran · Aug 23, 2014 at 10:03 PM
can you guys explain these a little bit more ? I need to open and close my doors too, but i really did not find a how to for it. :( All i need is to open the doors just like in counter strike so that when i walk around in my building model i can enter the rooms. That's all i want and it should not be that complex...
you know it is better to use (animator)animation for this... because cutting time seconds some time slow the system...ya i did experiment it....some time it gets lagy... use animation or a trick that i use some time
door.localEulerAngles.y=$$anonymous$$athf.Lerp(door.localEulerAngles.y,180,5*Time.deltaTime);
Answer by rick_rick · Jan 06, 2016 at 09:23 AM
You might try using colliders on the door and your first person camera to trigger an animation of the door opening. I'm assuming you're using a first person camera if you're doing a walk through of an environment.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Using Quaternions and C# script to animate a mesh 1 Answer
Loop Animation In Script? 2 Answers
Setting animation files' wrapmode 0 Answers
Rotating An Object On Its Axis Once 3 Answers