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?
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;
}