- Home /
Rotate an object a set angle over time? (C#)
Hello!
I have an object that I want to rotate 20 degrees, but I want it to rotate ONLY 20 degrees over a few seconds. I just cannot get it to work!
I've tried things like:
object.transform.Rotate(Vector3.right * (20 * Time.deltaTime));
But from what I understand, this just rotates it by 20 degrees every second and doesn't stop at 20. I tried using an "if (object.transform.rotation <=20)" with the above code but that didn't stop it either.
I have my object moving forwards in the z-axis with Lerp by using:
object.transform.position = new Vector3(object.transform.position.x, object.transform.position.y, Mathf.Lerp(object.transform.position.z, (destination.transform.position.z+10), Time.deltaTime));
That seems to work fine when moving an object a certain distance, but I tried to implement the same idea with rotation and I just can't do it. I get quite a lot of errors about Quaternions and Vectors can't be converted etc.
I don't fully understand Quaternions but I have a feeling I have to use them.
Any help would be much appreciated! Thanks alot!
EDIT: To rephrase the question, I don't have to be able to set how many seconds it takes to rotate. I just want it to gradually rotate, but stop after a certain amount of degrees.
Everything I try seems to just make it continuously rotate, but I just want it to stop rotating after a set amount of degrees.
Answer by whydoidoit · Mar 25, 2014 at 04:34 AM
The answer is to use a coroutine or a condition in your Update to uses a state to decide whether you should be rotating or not.
Coroutines are easy for this:
IEnumerator RotateMe(Vector3 byAngles, float inTime) {
var fromAngle = transform.rotation;
var toAngle = Quaternion.Euler(transform.eulerAngles + byAngles);
for(var t = 0f; t < 1; t += Time.deltaTime/inTime) {
transform.rotation = Quaternion.Lerp(fromAngle, toAngle, t);
yield return null;
}
}
Then to rotate by 20 degrees in 5 seconds - do this once when you want it to start
StartCoroutine(RotateMe(Vector3.right * 20, 5));
Thanks for your quick reply!
I've put it into my code, but it didn't work. It doesn't recognise StartCoroutine as a method.
I removed StartCoroutine so it just left me with Rotate$$anonymous$$e(Vector3.right * 20, 5); but still no luck!
Is it javascript? If so, I unfortunately have to have this in C# as it's an assignment! :(
IEnumerator Rotate$$anonymous$$e(Vector3 byAngles, float inTime)
{
Quaternion fromAngle = transform.rotation ;
Quaternion toAngle = Quaternion.Euler(transform.eulerAngles + byAngles) ;
for(float t = 0f ; t < 1f ; t += Time.deltaTime/inTime)
{
transform.rotation = Quaternion.Lerp(fromAngle, toAngle, t) ;
yield return null ;
}
}
-----
StartCoroutine(Rotate$$anonymous$$e(Vector3.right * 20f, 5f)) ;
i likes this code a rubicks cube game i was making problem being is the 20F the angle ...... i changed the 20f to 90f, and the 5f ( seconds) to 1f.. for a quicker rotation over 90 degrees.... simple enough.. but the rotation isn't exactly 90 degrees it just around and rotates a random number each time., so over time the cube bits unaligned them self's over time , so if this was and defiantly the degrees this would have been a brilliant code. i'm new to all this so if ya know of a way to help me out id really appreciate it ...
Why does this need to be done using a coroutine? Isn't that inefficient as coroutines require quite a bit of processing power?
Also I can't get it to rotate the exact angle using this code - its always a degree or so out =/
When I put it into my code, StartCoroutine is red and says:
"The name 'StartCoroutine' does not exist in the current context."
Do I need to add a 'Using' at the top of my code? Is there something I'm missing?
I'VE DONE IT! I used Quaternion.AngleAxis to get it to stop at 20 degrees! I floated these at the top:
public float totalRotation = 0;
public float rotationAmount = 20f;
Then I used Quaternion.AngleAxis to get it to stop at 20 degrees:
if($$anonymous$$athf.Abs (totalRotation) < $$anonymous$$athf.Abs (rotationAmount)){
float currentAngle = cube.transform.rotation.eulerAngles.x;
cube.transform.rotation = Quaternion.AngleAxis(currentAngle + (Time.deltaTime * 5), Vector3.right);
totalRotation += Time.deltaTime * 5;
}