- Home /
How to rotate object by X degrees with Y speed C#
Hi. I've been trying to get this to work for a few days now and couldn't find an exact answer for what I needed, after scouring Google and the Unity forums.
I'm trying to rotate an object by a certain amount of degrees, with a fixed speed. Looking on the forums I've found how to rotate an object just using time, but I need a rotation speed, not a time in which to rotate the object. The reason why I need this is because I'm setting the target rotation with a gesture (swiping) and then I need the object to follow rotating at a certain speed. Here's the code:
void Update ()
{
if (shouldRotate == true)
{
Rotate2 (rotateBy);
}
}
public void Rotate2(float rotationAngle)
{
Quaternion oldRotation = transform.rotation;
transform.Rotate(0,0,rotationAngle);
Quaternion newRotation = transform.rotation;
transform.rotation = Quaternion.Slerp(oldRotation, newRotation, Time.time * rotateSpeed);
shouldRotate = false;
}
At the moment I'm calling shouldRotate = true by pushing E from the controller class. The problem is that the object moves the amount of degrees IMMEDIATELY, not slowly. I've tried modifying the speed but the rotation is INSTANT, which leads me to believe there's a problem with my code. Let me know what you think.
Wiz
Answer by robertbu · Aug 03, 2014 at 10:58 PM
There are a several things wrong in this code, and without understand what you are trying to do, I cannot figure out how to redo this code so it gives you what you want. If 'rotationAngle' represents degrees per second, you can do:
transform.Rotate(0,0,rotationAngle * time.deltaTime);
Another way is to use Quaternion.RotateTowards(). If you execute something like the following each frame from Update():
transform.rotation = Quaternion.RotateTowards(transform.rotation, newRotation, Time.deltaTime * rotateSpeed);
But you cannot mixing and matching the two as you've done here both has problems, and any working result would be UGLY. Note the use of 'Time.deltaTime', not 'Time.time'.
In general if you know the destination of your rotation, use RotateTowards() and set the destination once (not repeatedly each frame). If you just want the object to spin, use Transform.Rotate().
Thank you for answering. I basically have an object that I want to rotate by X angles with a certain speed, NOT in a certain amount of time.
I did not know what was the best way to do it, I looked around the internet and the result is a mix of what I found as I applied it to my problem. If you have another suggestion, do let me know.
I want to rotate by X angles with a certain speed
You can never know too much before picking the best rotation solution. For example here are other factors:
Does the code need to handle rotations greater than 180 degrees
Are all the rotations to be axes aligned with either the local axes or the global axes?
Does you need the ability to change or interrupt the rotation while it is still rotating?
Here is a script with two different versions of RotateBy(). The first will handle rotations where the amount rotated by is greater than 180 degrees. The second is a bit simpler and always takes the shortest path between the start and end rotations. Neither are currently changeable or interrupt-able. You should be able to drop either coroutine into your class and call them with:
RotateBy(rotateBy, transform.forward, rotateSpeed);
In a new scene, put the script below on an object and hit space while it is running.
using UnityEngine;
using System.Collections;
public class Example : $$anonymous$$onoBehaviour {
bool rotating = false;
void Update() {
if (!rotating && Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space)) {
Vector3 axis = Random.rotation * Vector3.up;
float angle = Random.Range (-125.0f, 125.0f);
float speed = Random.Range (45.0f, 135.0f);
Debug.Log ("Rotating by " + angle + " degrees on axis " + axis + " at " + speed + " per second.");
StartCoroutine(RotateBy(angle, axis, speed));
}
}
IEnumerator RotateBy(float angle, Vector3 axis, float speed) {
rotating = true;
Quaternion start = transform.rotation;
float curAngle = 0.0f;
while ($$anonymous$$athf.Abs (curAngle - angle) > 0.0001f) {
curAngle = $$anonymous$$athf.$$anonymous$$oveTowards (curAngle, angle, Time.deltaTime * speed);
transform.rotation = Quaternion.AngleAxis (curAngle, axis) * start;
yield return null;
}
transform.rotation = Quaternion.AngleAxis (angle, axis) * start;
rotating = false;
}
IEnumerator RotateBy2(float angle, Vector3 axis, float speed) {
rotating = true;
Quaternion dest = Quaternion.AngleAxis (angle, axis) * transform.rotation;
while (transform.rotation != dest) {
transform.rotation = Quaternion.RotateTowards (transform.rotation, dest, Time.deltaTime * speed);
yield return null;
}
rotating = false;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613155726im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How rotate only z axis of Gameobject (2D) 1 Answer
Incrementing Rotation Produces Strange Transform Values 1 Answer
Cannot instantiate rotation on Prefab 1 Answer
Rotation with different speed for different axis 2 Answers
rotate an object on the z axis so that it does not exceed 0-45 degrees 0 Answers