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

public class MovementSpeedController : MonoBehaviour
{
    public List<Transform> objectsToMove = new List<Transform>();
    public List<Vector3> positions = new List<Vector3>();
    public int amountOfPositions = 30;
    public int minRandRange, maxRandRange;
    public bool randomPositions = false;
    public bool generateNewPositions = false;
    public float duration = 5f;
    public bool pingPong = false;

    private void Start()
    {
        if (generateNewPositions || (positions.Count == 0 && amountOfPositions > 0))
            GeneratePositions();

        StartCoroutine(MoveBetweenPositions(duration));
    }

    private void GeneratePositions()
    {
        for (int i = 0; i < amountOfPositions; i++)
        {
            if (randomPositions)
            {
                var randPosX = UnityEngine.Random.Range(minRandRange, maxRandRange);
                var randPosY = UnityEngine.Random.Range(minRandRange, maxRandRange);
                var randPosZ = UnityEngine.Random.Range(minRandRange, maxRandRange);
                positions.Add(new Vector3(randPosX, randPosY, randPosZ));
            }
            else
            {
                positions.Add(new Vector3(i, i, i));
            }
        }
    }

    IEnumerator MoveBetweenPositions(float duration)
    {
        for (int i = 0; i < positions.Count; i++)
        {
            float time = 0;
            Vector3 startPosition = objectsToMove[0].position;

            while (time < duration)
            {
                objectsToMove[0].position = Vector3.Lerp(startPosition, positions[i], time / duration);
                time += Time.deltaTime;
                yield return null;
            }

            objectsToMove[0].position = positions[i];
        }
    }
}

Now only the first object in objectsToMove is moving between the waypoints but I need all of the elements of objectsToMove to move between the waypoints. If I try this:

objectsToMove[i].position = Vector3.Lerp(startPosition, positions[i], time / duration);

I will let out of bound index exception because the size of objectsToMove is not the same the size of positions[i]

\$\endgroup\$
2
  • \$\begingroup\$ You can't use i on objectsToMove because i belongs to the positions iteration. It's iterating the count of positions. If that is to work, you'll need to have the same count of objects as the positions array. Do you want all the objects to go through the same positions together or go to random positions from the positions list? \$\endgroup\$
    – Dinuka Jay
    May 13, 2021 at 6:17
  • \$\begingroup\$ @DinukaJay random yes. \$\endgroup\$
    – Ben Gigi
    May 13, 2021 at 6:24

1 Answer 1

0
\$\begingroup\$

In my opinion best thing to do is:

To make one movement script and to attach it to each object, so that each object can run its own movement script separately. Then just fire moving command to each object, and pass them a path where you store all waypoints. It could look something like this:

 myGameObject.GetComponent<theMovingScript>().TakePathAndGoNow(positions);

Because now you are trying to iterate through waypoints, and also iterate through gameObjects, and things get messy quick.

Think of your game objects as , well .. separate objects and let each run its own logic. In Unity you can write one script and attach it to many objects - and now your objects can have their own speeds, go to different paths etc, even it is really only one script file.

In short: let Unity do the heavy work for you.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .