#StackBounty: #c# #unity3d #bezier How to make game objects follow a path in order in Unity along a bezier curve

Bounty: 50

I have a single Object moving along a Bezier Curve but i have multiple objects that need to follow this path in order but they all follow at the same time.The object is a snake type enemy in a space shooter game.

I have so far tried to make all objects children but in doing this they remain in a straight line to the parent when follow the Bezier Curve.
I have also made all objects separate and attached the Bezier Script to these so they all follow the same route and this works but only they follow the same path at the same time.

public class BezierFollow : MonoBehaviour{

private Transform[] routes;

private int routeToGo;

private float tParam;

private Vector2 enemyPosition;

public float speedModifier = 0.5f;

private bool coroutineAloud;

// Start is called before the first frame update
void Start()
    routeToGo = 0;
    tParam = 0f;
    //speedModifier = 0.5f;
    coroutineAloud = true;

// Update is called once per frame
void Update()
    if (coroutineAloud)

private IEnumerator GoByTheRouteRoutine(int routeNumber)
    coroutineAloud = false;

    Vector2 p0 = routes[routeNumber].GetChild(0).position;
    Vector2 p1 = routes[routeNumber].GetChild(1).position;
    Vector2 p2 = routes[routeNumber].GetChild(2).position;
    Vector2 p3 = routes[routeNumber].GetChild(3).position;

    while(tParam < 1)
        tParam += Time.deltaTime * speedModifier;

        enemyPosition = Mathf.Pow(1 - tParam, 3) * p0 +
            3 * Mathf.Pow(1 - tParam, 2) * tParam * p1 +
            3 * (1 - tParam) * Mathf.Pow(tParam, 2) * p2 +
            Mathf.Pow(tParam, 3) * p3;

        transform.position = enemyPosition;
        yield return new WaitForEndOfFrame();

    tParam = 0f;

    routeToGo += 1;

    if(routeToGo > routes.Length - 1)
        routeToGo = 0;

    coroutineAloud = true;


Here is the routes script i don’t think you will need but will include this

public class Route : MonoBehaviour{
private Transform[] controlPoints;

private Vector2 gizmosPos;

private void OnDrawGizmos()
    for(float t = 0; t <= 1; t += 0.05f)
        gizmosPos = Mathf.Pow(1 - t, 3) * controlPoints[0].position +
            3 * Mathf.Pow(1 - t, 2) * t * controlPoints[1].position +
            3 * (1 - t) * Mathf.Pow(t, 2) * controlPoints[2].position +
            Mathf.Pow(t, 3) * controlPoints[3].position;

        Gizmos.DrawSphere(gizmosPos, 0.25f);

    Gizmos.DrawLine(new Vector2(controlPoints[0].position.x, controlPoints[0].position.y),
        new Vector2(controlPoints[1].position.x, controlPoints[1].position.y));

    Gizmos.DrawLine(new Vector2(controlPoints[2].position.x, controlPoints[2].position.y),
       new Vector2(controlPoints[3].position.x, controlPoints[3].position.y));

I think what i need to do is have each object not be a child object and all have the script attached to follow the route but also have a delayed time before it follows along the path but not sure how to go about this. I was thinking this might need to be done in a separate script because in the bezier curve script it is set so the object starts again at the beginning of the route once reaches the end

Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.