0
\$\begingroup\$
public void duplicateObject(UnityEngine.GameObject original, int howmany)
    {
        howmany++;
        for (int i = 0; i < waypoints.Count - 1; i++)
        {
            for (int j = 1; j < howmany; j++)
            {
                Vector3 position = waypoints[i].transform.position + j * (waypoints[i + 1].transform.position - waypoints[i].transform.position) / howmany;
                UnityEngine.GameObject go = Instantiate(original, new Vector3(position.x, 0, position.z), Quaternion.identity);
                go.transform.localScale = lightsScale;
                objects.Add(go);
            }
        }
    }

For example howmany value is 21. If I have two waypoints it's fine it will create 21 spheres between the two waypoints.

But if I have 3 waypoints it's creating 42 spheres but it should create 21 since howmany is still 21. And it also position the spheres between waypoint 1 and 2 and then from waypoint 2 to 3.

But I want that it will create 7 spheres between waypoints 1 and 2 and 7 spheres between waypoints 2 and 3 and last 7 spheres between waypoints 3 and 1.

Lights

\$\endgroup\$
1
\$\begingroup\$

You'll need to divide howmany by the number of waypoints.

public void duplicateObject(UnityEngine.GameObject original, int howmany)
{
    if(waypoints.Count <= 1) {
        return; // check for division by zero
    }

    // add this division here
    howmany /= (waypoints.Count-1);

    if(howmany < 1){
        howmany = 1; // put at least 1 ball between waypoints ?
    }

    for (int i = 0; i < waypoints.Count - 1; i++)
    {
        // Changed the < to a <= instead of adding one.
        for (int j = 1; j <= howmany; j++)
        {
            Vector3 position = waypoints[i].transform.position + j * (waypoints[i + 1].transform.position - waypoints[i].transform.position) / howmany;
            UnityEngine.GameObject go = Instantiate(original, new Vector3(position.x, 0, position.z), Quaternion.identity);
            go.transform.localScale = lightsScale;
            objects.Add(go);
        }
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.