- Home /
Changing the speed of a rotating object
I am making a game in which I have a turret which rotates. Originally I had code which made it work but the speed was too slow:
//transform.parent.rotation =
// Quaternion.AngleAxis(currentAngle + (Time.deltaTime * rotationDegreesPerSecond), Vector3.forward);
Based on this question link text I changed my code to this:
rotationDegreesPerSecond = 90f
transform.parent.rotation =
Quaternion.Slerp (transform.parent.localRotation, Quaternion.AngleAxis(currentAngle + (Time.deltaTime * rotationDegreesPerSecond), Vector3.forward), Time.deltaTime * 2000000);
Now what I don't understand is where I have Time.deltaTime * 2000000. When I had it at Time.deltaTime *3 it rotated really slowly. But when I increased the number the rotation speed increased as well. Unfortunatly eventually it stopped speeding up no matter how high I put the number. It is now rotating about as fast as my original code but I need it to rotate about twice as fast or faster (currently approx 4 seconds for a full 360 degree rotation).
I would really appreciate it if someone could explain to me how to fix this or else explain a different way to do this.
thanks, Michelle
Answer by michellejean · Feb 06, 2014 at 10:53 AM
Ok I figured it out. This is my new code:
float rotateSpeed = 5f;
void Update(){
rotateCounterClockwise();
}
void rotateCounterClockwise(){
transform.parent.Rotate (Vector3.forward, rotateSpeed);
}
I was obviously way over complicating it.
Answer by robertbu · Feb 06, 2014 at 09:30 AM
I cannot give you and exact answer on what you should be doing without see more of your turret code, but I can point out some strangeness in your current code. The first parameter of your Slerp uses 'localRotation' but you are assigning 'rotation'. Depending on your use, they both should match. But because of the next issue, the use of localRotation is a non-issue since the first parameter is never used.
The second issue is how you are using your Slerp(). Used this way, the last parameter must be a fraction of 1.0. Anything above 1.0, and the code simply assigns the second parameter. In other words, your code is the same as:
transform.parent.rotation = Quaternion.AngleAxis(currentAngle + (Time.deltaTime * rotationDegreesPerSecond));
That is the Slerp() is not used and the second parameter is just getting assigned. With this in mind, you can speed up your rotation code by changing the 'rotationDegreesPerSecond' parameter. Without seeing the rest of your turret code and have a understanding of your setup, I cannot go further in recommending a solution. I expect the 'right' solution when you get things sorted out will use Quaternion.RotateTowards() instead of Slerp().
That actually is pretty much my entire script. The game objects are already in the scene and I just dropped the script on them. There are two of them and they both rotate as I planned just really slow. The code I posted here is currently in the update method, although I will be changing that eventually.
I changed the code to use Quaternion.RotateTowards();
transform.parent.rotation = Quaternion.RotateTowards(transform.parent.rotation, Quaternion.AngleAxis(currentAngle + (Time.deltaTime * rotationDegreesPerSecond), Vector3.forward), Time.deltaTime * 400f);
Unfortunately I'm having the same problem. Changing the amount Time.deltaTime is multiplied by changes the speed of the rotation, however after a certain point it never gets any faster.
Your answer
![](https://koobas.hobune.stream/wayback/20220613132703im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Change rotation of an object based on an HTC Vive Controller 1 Answer
Choose starting point/angle for rotation with cos and sin 0 Answers
What Should i use rigidbody.AddTorque() or Quaternions ? 1 Answer
Problem with Quaternion.LookRotation() 1 Answer
How to rotate an object depending on the rotation of an other Object 3 Answers