IEnumerator with transform.Rotate is slighty off

I am trying to rotate a object (with the help of a custom Easing Curve + IEnumerator), but the rotation is slightly off. A example of rotating 90deg results in a strange rotation of about ~100deg. I’ve spend two hours already, but I just can’t figure out where the exact error is and how to solve it

``````IEnumerator RotateCustom(GameObject target, float rotation=90f, float duration=1f){
float time = 0;

while (time < duration)
{
float t = time / duration;
float progress = t * t * t * (t * (t * 6f - 15f) + 10f); // EaseSmoothStep

target.transform.Rotate(new Vector3(0f, rotation / 360f * progress, 0f));
time += Time.deltaTime;

yield return null;
}
}
``````

Rotate works in local space by default. Perhaps that’s throwing you off, if the object is parented to something else that’s also been rotated?

If you’re using this on an object that has a parent, try moving the object to the scene root (so that it has no parent) and seeing if the behavior is different.

I might be missing something, but i dont follow the math and why the sum of the applied rotations should equal the desired amount. The logic may be sound, but Im missing it.

I run the following code in an online c# editor and the total rotation comes out to like 6.4 so im not sure how you got 100 degrees.

``````    public static void Main()
{
float rotation=90f;
float duration=1f;
float dt = 0.02f;
float totalRot = 0f;
for(float time=0; time <= duration; time = time + dt){
float t = time / duration;
float progress = t * t * t * (t * (t * 6f - 15f) + 10f);
float frameRot = rotation / 360f * progress;
totalRot = totalRot + frameRot;
}

Console.WriteLine(totalRot);

}
``````

also, it doesnt seem like the amount you rotate in a frame is really dependent on Time.deltaTime, but rather just how much time has passed since starting the sequence. is your motion stuttery/not smooth?

You’re doing `transform.Rotate` which is an additive operation, whilst you iterate from 0 to 1 on t. This makes no sense. You should be using something like `transform.rotation = Quaternion.Slerp(startingRotation, endRotation, progress);`

I originally thought that was the case, actually – I guess I should’ve just gone and checked, hah…

But I would expect it to go crazy in that case – repeatedly rotating by 5, then 10, then 15, then 20, then 25, […], then 90 degrees would cause way more than 100 degrees of total rotation!

I suspect that is indeed the case but due to the cyclical nature of rotations they’re just ending up with a net rotation of ~100 degrees at the end by chance.

Uhm, how do you get those numbers? Your rotation value is set to 90. You divide that by 360. So at max your rotation per frame is 0.25 in that case. Though since t is less than 1 you actually rotate by less each frame.

The whole rotation is also framerate depended. Only linear progress can be “fixed” by deltaTime. So your “time” does increase properly. However your rotation actually follows a quadratic curve. You essentially integrate deltaTime which is more or less a constant which gives you a linear progress. Then you integrate that time which gives you a quadratic behaviour. Though, your smoothing function would throw it off even more

Like @PraetorBlue said, the correct solution would be to use Quaternion.Slerp between the start and end rotation. Something like this:

``````// untested
IEnumerator RotateCustom(GameObject target, float rotation=90f, float duration=1f)
{
var start = target.rotation;
var end = Quaternion.Euler(0f, rotation, 0f) * start;
float factor = 1f / duration;
for (float t = 0f; t < 1f; t += Time.deltaTime * factor)
{
float progress = t * t * t * (t * (t * 6f - 15f) + 10f); // Ease out
target.transform.rotation = Quaternion.Slerp(start, end, progress);
yield return null;
}
target.transform.rotation = end;
}
``````

Ah, I missed the division by 360f. That makes a lot more sense now.

Thank you for all the answers, I’ve tried all suggested answers, but unfortunately this does not solve my issue, because I made the mistake of simplifying my original code too much in my first post.

So I will try to explain the related problem. I want to rotate a object (a rectangle, e.g. a door) around a specific pivot point/position. But when I try to change target.transform.position accordingly to the target.transform.rotation animation, then it seems like the movement is off / not in sync with the rotation, even if I use Slerp for both rotation and position. I’ve also tried another version with RotateAround(), and it rotates correctly around a specific position, but then I have problems applying custom lerp easings. I’ve uploaded a gif with the animation (see attachment). The code looks like this:

``````IEnumerator LerpRotateDoor(GameObject target, /*Quaternion startRotation, Quaternion endRotation,*/ float duration=1f)
{
// Test Values for a simple 90deg rotation around a specific pivot position
var startRotation = target.transform.rotation;
var endRotation = Quaternion.Euler(0f, -90f, 0f) * startRotation;
var startPosition = target.transform.position;
var endPosition = startPosition + new Vector3(target.transform.localScale.x / 2, 0, -(target.transform.localScale.x / 2));
float factor = 1f / duration;

for (float t = 0f; t < 1f; t += Time.deltaTime * factor)
{
float progress = t * t * t * (t * (t * 6f - 15f) + 10f); // Ease out
target.transform.rotation = Quaternion.Slerp(startRotation, endRotation, progress);
target.transform.position = Vector3.Slerp(startPosition, endPosition, progress);
yield return null;
}

target.transform.rotation = endRotation;
target.transform.position = endPosition;
}
``````

No, it’s completely in sync, however what you’re doing makes things just much more complicated than it needs to be. The issue here is that you actually want to rotate around a point at the edge of your door where you define your “hinge”. However instead you rotate the door around its center and manually shift the door around. This doesn’t make much sense. Since you “move” / lerp the center linearly to the target position, the door would not describe an “arc”.

You can simplify your whole logic by setting up some additional gameobjects. That way you can “configure” the door in the editor and the code becomes much simpler. A rotating door only rotates on a single axis. So all you have to do is place your “axis” at the right position and simply rotate that axis. I would highly recommend using two empty gameobjects. One represent the fix part of the hinge, the other nested object would represent the moving / rotating part of the hinge. Below that rotating object you can add your actual door object as a child. You can offset the door mesh inside the hinge object so the position of the hinge is where it should be. To open / close the door you only need to rotate the inner hinge object around a single axis. No manual position change / manipulation required. You also don’t need to use slerp as you can simply assign the localRotation and use `Quaternion.Euler(0,angle,0)` to specify the relative rotation within the outer “fixed hinge” parent object.

Of course to move the door where it needs to be, you just move the outer hinge object where it should be. So the same script can be used for any kind of rotating door. Since you can re-orient the outer hinge any way you like, you can even change the rotation axis. You probably want to give the script an target angle how far the door should rotate, so it could be changed depending on the usage.

Here’s an example script that provides 3 methods to open / close / toggle the door:

``````using System.Collections;
using UnityEngine;

public class RotatingDoor : MonoBehaviour
{
public Transform child;
public float ClosedAngle = 0;
public float OpenAngle = 90;
public float duration = 1f;
private bool m_Moving = false;
private bool m_Open = false;
private float m_CurrentAngle = 0;

void Awake()
{
SetAngle(ClosedAngle);
}
void SetAngle(float aAngle)
{
m_CurrentAngle = aAngle;
child.localEulerAngles = new Vector3(0, m_CurrentAngle, 0);
}
IEnumerator MoveDoor(bool aOpen)
{
if (m_Open == aOpen)
yield break;
m_Moving = true;
float startAngle = m_CurrentAngle;
float targetAngle = aOpen ? OpenAngle : ClosedAngle;
float factor = 1f / duration;

for (float t = 0f; t < 1f; t += Time.deltaTime * factor)
{
float progress = t * t * t * (t * (t * 6f - 15f) + 10f);
SetAngle(Mathf.Lerp(startAngle, targetAngle, progress));
yield return null;
}
m_Open = aOpen;
SetAngle(targetAngle);
m_Moving = false;
}
public void OpenDoor()
{
if (!m_Moving && !m_Open)
StartCoroutine(MoveDoor(true));
}
public void OpenClose()
{
if (!m_Moving && m_Open)
StartCoroutine(MoveDoor(false));
}
public void ToggleDoor()
{
if (!m_Moving)
StartCoroutine(MoveDoor(!m_Open));
}
}
``````

Note that we can’t really interrupt the animation. So we can only close the door once it’s fully open and vice versa. If you want to be able to switch direction in between, it would be better to not use a coroutine at all. Instead something like this would make the most sense:

``````using System.Collections;
using UnityEngine;

public class RotatingDoor : MonoBehaviour
{
public Transform child;
public float ClosedAngle = 0;
public float OpenedAngle = 90;
public float duration = 1f;
private float m_CurrentPos = 0;
private float m_Direction = -1;

void Awake()
{
SetPos(0);
}
void SetPos(float aCurrentVal)
{
float t = m_CurrentPos = Mathf.Clamp01(aCurrentVal);
t = t * t * t * (t * (t * 6f - 15f) + 10f);
child.localEulerAngles = new Vector3(0, Mathf.Lerp(ClosedAngle, OpenedAngle, t), 0);
}
private void Update()
{
SetPos(m_CurrentPos + Time.deltaTime * m_Direction / duration);
if (m_CurrentPos == 1 || m_CurrentPos == 0)
{
enabled = false;
}
}
public void OpenDoor()
{
enabled = true;
m_Direction = 1;
}
public void OpenClose()
{
enabled = true;
m_Direction = -1;
}
public void ToggleDoor()
{
enabled = true;
m_Direction = -m_Direction;
}
}
``````

Here we simply either increase or decrease the lerp variable accordingly to the given direction. The script will disable itself once it reaches one of the two ends. So the Update only runs while the door is rotating. We can simply change the direction at any time which would reverse the rotation from its current position.

Just in case it’s not clear, “child” would be the child object that should actually rotate. So you need to assign that in the inspector. That child would rotate inside the parent object around the parents up axis. You can now parent the actual door to that child object and position the door in such a way it rotates the way you want. The closed angle should usually be 0. That makes it easier to position the door. Since the angles are not constraint and we simply lerp between them, we could even model something like a valve where we could set a closed angle of 0 and an open angle of 1800 (5 full rotations). Set the duration to 3 seconds and you get a neat rotating animation. Such generic scripts can be used for all sorts of things.

Though for more complex animations, it would be the best to actually create an animation. That’s why Unity has an animation system