- Home /
Way to Move/Rotate object through List of points that would work with changing timescale
Hi all,
I've been trying to think of a way to do this for too many hours now.. I simply want a way to smoothly move an object through a list of positions (this I sort of have already) and for this to be affected when timescale and fixedDeltaTime are changed.
I currently have a simple coroutine which when activated by a keydown iterates through the list of positions and sets them appropriately:
for (int i = targetPos.Count - 1; i > -1; i--)
{
transform.position = targetPos[i];
transform.rotation = targetRot[i];
}
This obviously will not be affected by slowing down time. So I tried replacing it with lerping or movetowards'ing but that either didn't work at all or gave some crazy non-results. My last attempt to do this looks like this (excuse my probably obvious missunderstanding of how coroutines work, but by now I've been trying all sorts of craziness):
...(inside coroutine)...
for (int i = targetPos.Count - 1; i > -1; i--)
{
StartCoroutine(MovePos(targetPos[i], targetRot[i]));
}
...(nested coroutine)...
IEnumerator MovePos (Vector3 endP, Quaternion endR)
{
while (transform.position != endP || transform.rotation != endR)
{
transform.position = Vector3.MoveTowards(transform.position, endP, Time.deltaTime);
transform.rotation = Quaternion.RotateTowards(transform.rotation, endR, Time.deltaTime);
}
if(transform.position == endP && transform.rotation == endR)
{
yield break;
}
}
This looks exactly the same as the simple method when played.
Any ideas?
Answer by slavo · Nov 25, 2016 at 09:47 AM
Hello,
in your Coroutine you are Lerping correctly, but everything in While loop happening in one frame. You need to wait for another frame in each iteration.
while (transform.position != endP || transform.rotation != endR)
{
transform.position = Vector3.MoveTowards(transform.position, endP, Time.deltaTime);
transform.rotation = Quaternion.RotateTowards(transform.rotation, endR, Time.deltaTime);
//WAIT FOR ANOTHER FRAME
yield return null;
}
Right, stupid of me to miss that, I had a similar setup when I wasn`t nesting coroutines.. But now the whole thing churns out *some crazy results* as mentioned in the original post. From what it looks like maybe the for loop iterates down the list once the nested coroutine is yielded and so the object doesn't get to move the whole way through to the target position before the new target is assigned? The object definitely does not reach its final position.
Because of all that I can't even begin to test whether scaling down timescale works.
No idea what to do
When you want your coroutine to wait for nested coroutine you need to yield retrun start coroutine. This way you start coroutine and you continue for loop. so you have 'targetPos.Count - 1' corutines running at same time and messing with you transform.
...(inside coroutine)...
for (int i = targetPos.Count - 1; i > -1; i--)
{
yield return StartCoroutine($$anonymous$$ovePos(targetPos[i], targetRot[i]));
}
alright alright alright! It's working, thank You for that, I'm marking it as an answer, but the movement I'm getting is jittery and unfortunately I've no idea why..
It's less noticeable when slowed down so I could potentially hide it as i'm only using this when time's slow, but that wouldn't really meet the "polish" level I want. Any ideas? If not, thank You anyway for sorting the basics out!
Your answer
![](https://koobas.hobune.stream/wayback/20220612101327im_/https://answers.unity.com/themes/thub/images/avi.jpg)