I have a gameobject that rotates constant. I can start and stop the rotation. The problem is that it start or stops not smoothly. This is the code i use:
if (RotationTrigger.active) {
transform.Rotate (Vector3.up, speed * Time.deltaTime); print("Object is rotating");
}
else {
transform.Rotate (Vector3.up, nospeed * Time.deltaTime); print("Object is not rotating");
I can’t figure out how to make a smooth transition form the rotation speed to nospeed / nospeed to speed. I tried iTween but i’m stucked. This is the code i tried with iTween:
if (RotationTrigger.active) {
float speed01 = (speed * Time.deltaTime);
iTween.RotateAdd(gameObject, iTween.Hash("speed", speed01, "easetype", "easeInCubic"));
}
else {
transform.Rotate (Vector3.up, nospeed * Time.deltaTime); print("Object is not rotating");
If you want to achieve a smooth rotation between two points there are a few ways of going about that, I would use Euler Angles and use the Quaternion.Slerp() method to rotate between to points. Slerp works in a similar way to Lerp but with rotation. Another way to achieve that effect it by using the RotateTowards() method.
Joshmond thanks for your response. The thing is, there are no specific points (from > to). It just rotates and stop when you want. At that point it has to stop smoothly or when it starts again start smoothly.
You can use a float to hold the speed. When you want to start or stop, you can use Mathf.Lerp to gradually increase or decrease the speed variable. When you want to change from stopped or rotated to stop, assign a float the current time, then, each frame, leap the speed of rotation down or up for however long you want the acceleration / deceleration to take.
How would I do that in my code here? Are you saying lerp this step variable then put it into the RotateTowards?
public void Rotation()
{
// The step size is equal to speed times frame time.
float step = rotationSpeed * Time.deltaTime;
// Rotate our transform a step closer to the target's.
playersLookDirection.transform.rotation = Quaternion.RotateTowards(playersLookDirection.transform.rotation, rotationTarget.rotation, step);
}