0
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class Waypoints : MonoBehaviour
{
    public List<Transform> objectsToMove;
    public LineRenderer lineRenderer;
    public float speed;
    public bool go = false;
    public bool moveToFirstPositionOnStart = false;
    public float rotSpeed;
    public bool random = false;
    public int currentCurvedLinePointIndex;

    private Vector3[] positions;
    private Vector3[] pos;
    private int index = 0;
    private bool goForward = true;
    private List<GameObject> curvedLinePoints = new List<GameObject>();
    private int numofposbetweenpoints;
    private bool getPositions = false;
    int randomIndex;
    int curvedPointsIndex;
    public float delay;
    private float[] startTimes;

    // Start is called before the first frame update
    void Start()
    {
        for (int i = 0; i < objectsToMove.Count; i++)
        {
            var parent = GameObject.Find("Moving Object Parent");
            objectsToMove[i] = Instantiate(objectsToMove[i], parent.transform);
            objectsToMove[i].name = "Platfrom";

            curvedLinePoints = GameObject.FindGameObjectsWithTag("Curved Line Point").ToList();

            if (curvedLinePoints != null && curvedLinePoints.Count > 0)
            {
                objectsToMove[i].rotation = curvedLinePoints[1].transform.rotation;
            }
        }

        startTimes = new float[objectsToMove.Count];

        for (int i = 0; i < startTimes.Length; i++)
        {
            startTimes[i] = Time.time + (Random.value * delay);
        }
    }

    Vector3[] GetLinePointsInWorldSpace()
    {
        positions = new Vector3[lineRenderer.positionCount];
        //Get the positions which are shown in the inspector 
        lineRenderer.GetPositions(positions);


        //the points returned are in world space
        return positions;
    }

    // Update is called once per frame
    void Update()
    {
        

        if (lineRenderer.positionCount > 0 && getPositions == false && CurvedLineRenderer.linesSet)
        {
            pos = GetLinePointsInWorldSpace();
            numofposbetweenpoints = curvedLinePoints.Count;

            if (moveToFirstPositionOnStart == true)
            {
                foreach (Transform objToMove in objectsToMove)
                {
                    objToMove.position = pos[index];
                }
            }

            getPositions = true;
        }

        if (go == true && lineRenderer.positionCount > 0)
        {
            Move();
        }
    }

    int counter = 0;
    int c = 1;
    void Move()
    {
        for (int i = 0; i < objectsToMove.Count; i++)
        {
            if (Time.time < startTimes[i]) continue;

            Vector3 newPos = objectsToMove[i].position;
            float distanceToTravel = speed * Time.deltaTime;

            bool stillTraveling = true;
            while (stillTraveling)
            {
                Vector3 oldPos = newPos;

                newPos = Vector3.MoveTowards(oldPos, pos[index], distanceToTravel);

                distanceToTravel -= Vector3.Distance(newPos, oldPos);
                if (newPos == pos[index]) // Vector3 comparison is approximate so this is ok
                {
                    // when you hit a waypoint:
                    if (goForward)
                    {
                        bool atLastOne = index >= pos.Length - 1;
                        if (!atLastOne)
                        {
                            index++;
                            counter++;
                            if (counter == numofposbetweenpoints)
                            {
                                c++;

                                counter = 0;
                            }
                            if (c == curvedLinePoints.Count - 1)
                            {
                                c = 0;
                            }
                        }
                        else { index--; goForward = false; }
                    }
                    else
                    { // going backwards:
                        bool atFirstOne = index <= 0;
                        if (!atFirstOne)
                        {
                            index--;

                            counter++;
                            if (counter == numofposbetweenpoints)
                            {
                                c++;

                                counter = 0;
                            }
                            if (c == curvedLinePoints.Count - 1)
                            {
                                c = 0;
                            }
                        }
                        else { index++; goForward = true; }
                    }
                }
                else
                {
                    stillTraveling = false;
                }
            }

            objectsToMove[i].position = newPos;
        }
    }
}

For example, I have 3 objects to move. The first one start moving after 5 seconds and moving on the pos array positions.

Then the second object is start moving after 7 seconds but now instead moving also on the pos array positions the second object is reaching the first moving object and then move with him together.

The same thing happens with the last third object to move from the List objectsToMove.

I can't figure out why they are reaching the first object that move instead that each one will move on the pos array positions ?

Not sure if it's the delay line inside the Move function that make the problem :

if (Time.time < startTimes[i]) continue;

Or maybe the loop in the Move function :

for (int i = 0; i < objectsToMove.Count; i++)
\$\endgroup\$
14
  • \$\begingroup\$ This is still the same conceptually-backwards approach from your last question. Why aren't you using an approach based on the accepted answer from that question? gamedev.stackexchange.com/questions/191643/… \$\endgroup\$
    – Kevin
    Commented Apr 21, 2021 at 0:59
  • \$\begingroup\$ To repeat what I said before - waypoints should not move objects. Objects should move themselves along waypoints. You're confusing yourself and making your code needlessly complicated by using this backwards approach. \$\endgroup\$
    – Kevin
    Commented Apr 21, 2021 at 1:11
  • \$\begingroup\$ @Kevin I tried your answer 2 days ago but I had to change it to work with my waypoints and it didn't work with the speed. I'm getting the waypoints from a LineRenderer lines count positions. The object/s are moving along the LineRenderer lines positions. I packed my very small project of the waypoints at this link :1drv.ms/u/s!AtV2OUzEcRzrxXOZ53REKxs59JLD?e=MOWwoS \$\endgroup\$
    – Daniel Lip
    Commented Apr 21, 2021 at 1:47
  • \$\begingroup\$ @Kevin there are 3 scripts. The waypoints another script that build create the waypoints positions from the LineRenderer and another script that make the lines of the LineRenderer smooth so the objects can be move at any speed on the positions of the line renderer. And with my waypoints script I calculate the positions and can move the objects along the waypoints at any speed. The idea to make multiple objects to move along the waypoints and adding delay came after. \$\endgroup\$
    – Daniel Lip
    Commented Apr 21, 2021 at 1:48
  • \$\begingroup\$ @Kevin You solution 2 days ago is fine but it didn't work with my waypoints system. I move objects over LineRenderer positions. I'm getting the LineRenderer positions from lines I draw and then move the objects over this positions in the array pos. \$\endgroup\$
    – Daniel Lip
    Commented Apr 21, 2021 at 1:50

0

You must log in to answer this question.

Browse other questions tagged .