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

public class Waypoints : UnityEngine.MonoBehaviour
{
    public List<GameObject> waypoints = new List<GameObject>();
    public Animator _animator;
    public int waitTimeBeforeGo;
    public int num = 0;
    public float minDist;
    public float speed;
    public bool rand = false;
    public bool go = true;
    public bool loop = false;
    public bool waitTime = false;
    public float smoothRotation;

    private bool lastPoint = false;

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        float dist = Vector3.Distance(gameObject.transform.position, waypoints[num].transform.position);

        if (waitTime == true)
        {
            StartCoroutine(WaitBeforeGo());
        }
        else
        {
            if (go)
            {
                //_animator.SetBool("Walk", true);

                if (dist > minDist && lastPoint == false)
                {
                    Move();
                }
                else
                {
                    if (!rand)
                    {
                        if (num + 1 == waypoints.Count)
                        {
                            if (loop == true)
                            {
                                num = 0;
                            }

                            lastPoint = true;
                            //_animator.SetBool("Idle", true);
                        }
                        else
                        {
                            num++;
                        }
                    }
                    else
                    {
                        num = Random.Range(0, waypoints.Count);
                    }
                }
            }
        }
    }

    public void Move()
    {
        Quaternion lookOnLook =
 Quaternion.LookRotation(waypoints[num].transform.position - transform.position);

        transform.rotation =
        Quaternion.Slerp(transform.rotation, lookOnLook, Time.deltaTime * smoothRotation);

        gameObject.transform.position += gameObject.transform.forward * speed * Time.deltaTime;
    }

    IEnumerator WaitBeforeGo()
    {
        yield return new WaitForSeconds(waitTimeBeforeGo);

        waitTime = false;
    }
}

For some reason when it's getting to this part it keep doing this part over and over again like in a endless loop and never continue :

if (num + 1 == waypoints.Count)
                            {
                                if (loop == true)
                                {
                                    num = 0;
                                }
    
                                lastPoint = true;

It keep doing the lines num = 0 and lastPoint = true all the time in a loop and never continue to the rest of the code.

For example there is only 1 waypoint it's moving to the waypoint but never get back it's staying at the waypoint. The loop(ping pong) is never working.

\$\endgroup\$
1
  • \$\begingroup\$ Why are you so sure that's where the issue is? Did you do stepthroughs? Just reading it I expect the game to get stuck on WaitBeforeGo(), potentially... \$\endgroup\$
    – Weckar E.
    Nov 29, 2020 at 9:04

1 Answer 1

1
\$\begingroup\$

For example there is only 1 waypoint it's moving to the waypoint but never get back it's staying at the waypoint. The loop(ping pong) is never working.

How can it loop if there's only one waypoint? Where else would it go?

When you can't figure out why your code doesn't work the way you want, usually one of the first things you should do is add some logging code that will help you understand what's going on. For example:

if (num + 1 == waypoints.Count) {
    Debug.Log("Reached final waypoint at index " + num + ", looping back to index 0");

Then if your output looks like this

Reached final waypoint at index 0, looping back to index 0

you know the problem is that there aren't any other waypoints to travel to.

\$\endgroup\$

You must log in to answer this question.

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