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
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);
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 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.