- Home /
Rotate object on the Y axis 90 degrees every 5 minutes?
Hello, Ive been trying to write a script which rotates an object 90 degrees on the Y axis after an amount of time has passed but I cant figure out what it is I have to do. This is my attempt but im far off what Im trying to achieve, can someone help me out please?
public var speed = 55.0;
private var rotation = 0.0;
private var qTo = Quaternion.identity;
function Update () {
if (Input.GetButtonDown("f")) {
rotation += 90.0;
qTo = Quaternion.Euler(0.0, 0.0, rotation);
}
transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, speed * Time.deltaTime);
}
Answer by Eno-Khaon · May 26, 2015 at 07:00 PM
I'm going to take your "90 degrees every 5 minutes" literally here:
// Javascript
public var fiveMinutes: float = (60.0 * 5.0); // 300 seconds
public var degreesPerTime: float = 90.0; // 90 degrees per 5 minutes
public var rotationAxis: Vector3 = Vector3.up; // Y-axis
function Update()
{
transform.rotation *= Quaternion.AngleAxis((degreesPerTime / fiveMinutes) * Time.deltaTime, rotationAxis);
}
If you're looking for a quick rotation to occur after every 5-minute interval has elapsed, you'll want a rolling timer to compare against Time.time and you'll want to use Quaternion.Lerp() to guarantee accurate stopping points for the rotations at a time.
Thanks, yeah I worded my question badly Im trying to make an object rotate 90 degrees at 5 $$anonymous$$ute intervals. Ive wrote this script however I do not fully understand how to use Quaternion.Lerp() since it needs a "from" and "to" transform where I dont know what to write. How do I alter this to make it work correctly?
var speed = 0.1;
InvokeRepeating("Rotate", 0, 2);
function Rotate () {
transform.rotation = Quaternion.Lerp(????, Time.time * speed);
}
The rotation you would want is your initial rotation when the spin is going to start, then multiply that by the AngleAxis rotation when for your ending rotation:
// Javascript
public var five$$anonymous$$inutes: float = (60.0 * 5.0); // 300 seconds
public var degreesPerRotation: float = 90.0; // 90 degree rotations every 5 $$anonymous$$utes
public var rotationAxis: Vector3 = vector3.up; // Y-axis
public var timeToSpin: float = 1.0; // 1 second spent spinning 90 degrees
private var rotateTimeStamp: float = 0.0; // $$anonymous$$ark the start of a spin
private var rotationStart: Quaternion; // Start of rotation
private var rotationEnd: Quaternion; // End of Rotation
function Start()
{
StartCoroutine(DelayedSpin);
}
function DelayedSpin()
{
while(true) // Another way to just keep going. (only applicable when used in conjunction with "yield" statements)
{
yield WaitForSeconds(five$$anonymous$$inutes - timeToSpin); // Total time $$anonymous$$us time spent, so the whole cycle starts every five $$anonymous$$utes
rotationStart = transform.rotation;
rotationEnd = transform.rotation * Quaternion.AngleAxis(degreesPerRotation, rotationAxis); // QuaternionA * QuaternionB is a concatenation of rotations.
rotateTimeStamp = Time.time;
while(Time.time - rotateTimeStamp < timeToSpin)
{
transform.rotation = Quaternion.Lerp(rotationStart, rotationEnd, (Time.time - rotateTimeStamp) / timeToSpin); // or Slerp for a smoother acceleration in and out of the rotation
yield; // Wait for next frame
}
transform.rotation = rotationEnd; // $$anonymous$$ake sure it ends on cue
}
}
This is untested, but should be more what you were looking for now, I think. ;)
Cheers for this, got this bad boy very nearly perfect however how do I change the speed at which the object spins? At the moment increasing the "timeToSpin" variable makes the rotation occur faster and decreasing it makes it just snap to the different rotation. Any ideas? I also tweaked the script a wee bit..
public var spinInterval: float = 60;
public var degreesPerRotation: float = 90.0;
public var rotationAxis : Vector3 = Vector3.up;
public var timeToSpin: float = 1.0; // 1 second spent spinning 90 degrees
private var rotateTimeStamp: float = 0.0;
private var rotationStart: Quaternion;
private var rotationEnd: Quaternion;
function Start()
{
DelayedSpin();
}
function DelayedSpin()
{
while(true)
{
yield WaitForSeconds(spinInterval - timeToSpin);
rotationStart = transform.rotation;
rotationEnd = transform.rotation * Quaternion.AngleAxis(degreesPerRotation, rotationAxis);
rotateTimeStamp = Time.time;
while(Time.time - rotateTimeStamp < timeToSpin)
{
transform.rotation = Quaternion.Lerp(rotationStart, rotationEnd, (Time.time - rotateTimeStamp) * timeToSpin);
yield;
}
transform.rotation = rotationEnd;
}
}
Oh, whoops! $$anonymous$$y mistake there. It was supposed to be a division by timeToSpin rather than multiplication. I've updated my previous comment to include this line ins$$anonymous$$d:
transform.rotation = Quaternion.Lerp(rotationStart, rotationEnd, (Time.time - rotateTimeStamp) / timeToSpin);
(Time.time - rotateTimeStamp) gives a single-second rotation time baseline. Divide that by the intended duration to get it to last longer. Sorry about that, I just got that one mixed up.
Your answer
Follow this Question
Related Questions
Rotate Object to face 2 axis? 1 Answer
How do we write a C# script to smoothly rotate objects based on Input.GetAxis("Mouse *")? 1 Answer
Add force at position Question 1 Answer
Rotate rigidbody using MoveRotation() around the global y axis 2 Answers
Make a cube rotate in right or left direction randomly 3 Answers