- Home /
Move Rigidbody along curve
Hi,
I've been stuck on this for a little while now. I have made a curve in my scene, and I have 50 locations along the curve. The thing I want to achieve is that my Rigidbody GameObject moves along this curve. I could set the Rigidbody location to the next location on the curve, but how would I move it along the curve with a certain speed?
Answer by lassade · Jan 22, 2018 at 03:13 AM
I just got at work and see that we use GoKit for the sake of simplicity it's great and free check it out GoKit Github
The pseudocode coroutine bellow will work if the moviment starts from the beginig or the end of the path granted you specify the moviment direction and the right stating location (pathIndex). By changing the velocity (accelerating or slowing down) the moviment could be made more natural
pathIndex = 0;
pathDirection = 1; // or -1 if backwards
while (true)
{
myDesiredLocation = pathPositions[pathIndex].position
myObjectTransform.position = Vector3.MoveTowards(myObjectTransform.position, myDesiredLocation, velocity * Time.deltaTime);
if (Vector3.Approximately(myObjectTransform.position, myDesiredLocation))
{
// My object reach the of path
pathIndex += pathDirection; // My next location in the path
if (pathIndex <= 0 || pathIndex >= pathPositions.Count) {
// stop doing the above because the player reach the end of the path
yeild break;
}
}
yeild return null;
}
Thanks for your response, I tried the code but the Rigidbody seems to get stuck the moment it gets into the actual curve. This is the result I've been getting from it. Any clue why this is happening? https://streamable.com/dqixz
Check my update in the anwser: "I use Go$$anonymous$$it in my job and its great!" Github Page Sry for overcomplicate things.
Oh, I didn't even realize that existed. Thanks a lot for your help!