-1
\$\begingroup\$

I have 5 waypoints ,each waypoint tagged and named as "Teleporter".

The Mover the blue cube is start moving to the first waypoint and then should move between all the other waypoints. but it's stopping on the waypoint before the last one.

Waypoints

And this screenshot show when the Mover stopped. The red's are the waypoints the Mover already visited.

Waypoints

This script is attached to the Mover :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Waypoints : MonoBehaviour
{
    public float speed;
    public bool go = false;
    public bool moveToFirstPositionOnStart = false;
    public List<Transform> positions;

    private int index = 0;

    // Start is called before the first frame update
    void Start()
    {
        if (moveToFirstPositionOnStart == true)
        {
            transform.position = positions[index].position;
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (go == true)
        {
            Move();
        }
    }

    void Move()
    {
        transform.position = Vector3.MoveTowards(transform.position,
                                                positions[index].position,
                                                speed * Time.deltaTime);

        if (transform.position == positions[index].position)
        {
            //index += 1;
        }

        if (index == positions.Count)
        {
            index = 0;
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Teleporter")
        {
            other.GetComponent<Renderer>().material.color = Color.red;

            index += 1;
        }
    }
}

I first tried to increase the index by 1 in this checking part :

if (transform.position == positions[index].position)
        {
            //index += 1;
        }

but it didn't reach the last waypoint so I tried and this is what I want to do in general to increase the index inside the OnTriggerEnter event but the same problem.

It's never reaching the last waypoint.

I also notice that when it's reaching the fourth waypoint and stop there that it's giving exception : ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. on the line :

transform.position = Vector3.MoveTowards(transform.position,
                                                    positions[index].position,
                                                    speed * Time.deltaTime);

Tested it again and this is working fine with this changes but with the OnTriggerEnter it's not working something inside the OnTriggerEnter make a problem not sure why and what.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Waypoints : MonoBehaviour
{
    public float speed;
    public bool go = false;
    public bool moveToFirstPositionOnStart = false;
    public List<Transform> positions;

    private int index = 0;

    // Start is called before the first frame update
    void Start()
    {
        if (moveToFirstPositionOnStart == true)
        {
            transform.position = positions[index].position;
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (go == true)
        {
            Move();
        }
    }

    void Move()
    {
        transform.position = Vector3.MoveTowards(transform.position,
                                                positions[index].position,
                                                speed * Time.deltaTime);

        float distance = Vector3.Distance(transform.position, positions[index].position);
        if (distance < 0.01f)
        {
            other.GetComponent<Renderer>().material.color = Color.red;

            index += 1;
        }

        if (index == positions.Count)
        {
            index = 0;
        }
    }
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Well, you get the exact error what went wrong. Your index counts to high and you try to access an element in the array that does not exist, your script crashes (and stops moving).

Now why it does not work with your OnTriggerEnter is, it counts the index to 5. And in your update you try first to move to index 5 in your List (which does not exist since it has only 5 entries and lists are zero based).

If you want to have it working with your OnTriggerEnter, either put this piece

if (index == positions.Count) {
   index = 0;
}

at the start in your Move or at the end in your OnTriggerEnter condition.

\$\endgroup\$

You must log in to answer this question.

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