Question by
Airgiraffe · Jan 24, 2021 at 11:48 PM ·
movementrotatelerpcoroutinesanimationcurve
Rotating object with Lerp in coroutine with correct speed
I have this running as a coroutine.
public IEnumerator Rotate(GameObject moveThis, GameObject target)
{
Vector3 vectorToTarget = target.transform.position - moveThis.transform.position;
float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
angle -= 90;
Quaternion newRotation = Quaternion.AngleAxis(angle, Vector3.forward);
if (moveThis.transform.rotation != newRotation)
{
float timer = 0.0f;
while (timer <= time)
{
moveThis.transform.rotation = UnityEngine.Quaternion.Lerp(moveThis.transform.rotation, newRotation, rotateAC.Evaluate(timer / time));
timer += Time.deltaTime;
yield return null;
}
}
moveThis.transform.rotation = newRotation;
}
It's triggered in a similar coroutine
public IEnumerator Move(GameObject moveThis, GameObject target)
{
yield return StartCoroutine(Rotate(moveThis, target));
UnityEngine.Vector3 pos1 = moveThis.transform.position;
UnityEngine.Vector3 pos2 = new UnityEngine.Vector3(target.transform.position.x,
target.transform.position.y,
moveThis.transform.position.z);
float timer = 0.0f;
while (timer <= moveTime)
{
timer += Time.deltaTime;
moveThis.transform.position = UnityEngine.Vector3.Lerp(pos1, pos2, moveAC.Evaluate(timer / moveTime));
yield return null;
}
moveThis.transform.position = pos2;
if (target == moveNodeOne)
{
moveNodeOne = null;
}
if (target == moveNodeTwo)
{
moveNodeTwo = null;
}
if (target == destination)
{
StartCoroutine(ResetRotate(moveThis));
EndMovement();
}
}
Both the animation curves and time/moveTime variables are set in the editor. Changing moveTime for the Move coroutine changes the speed at which the object moves, but changing the time for the Rotate coroutine simply adds a delay after the rotation has completed, and the rotation happens at the same speed regardless.
Otherwise, it's all working perfectly. How can I make it so that I can change the rotation time, removing the delay period between rotating finishing and then moving.
Thanks
Comment