Why the object never move between the waypoints?

by Daniel Lip   Last Updated March 13, 2018 01:13 AM

In the inspector I set the waypoints array to size 3 (three spheres). Then set the instancesToMove array size to 1.

Three waypoints and one object that should move between them minSpeed set to 2 maxSpeed to 5

The PathCompleteBehaviour is set to Stop. The object should move between the waypoints and stop at the last one. But the object is not moving at all.

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

public class Waypoints : MonoBehaviour
{
    // Adding selectable behaviour at the end of the path,
    // as requested from the comments.
    public enum PathCompleteBehaviour
    {
        Stop,
        Loop,
        Reverse
    }
    public PathCompleteBehaviour completeBehaviour;

    public GameObject[] waypoints;
    public GameObject[] instancesToMove;
    public float minSpeed, maxSpeed;

    // Store parallel arrays for our movement parameters.
    private float[] movementSpeeds;    // Randomized speed per object.
    private int[] nextWaypointIndices; // Destination per object.

    private void Start()
    {
        // Changing terminology to be more standard.
        // - a "prefab" is a source asset you want to copy
        // - an "instance" is a copy of it that's been spawned in the scene
        instancesToMove = GameObject.FindGameObjectsWithTag("InstanceToMove");

        movementSpeeds = new float[instancesToMove.Length];
        nextWaypointIndices = new int[instancesToMove.Length];

        for (int i = 0; i < instancesToMove.Length; i++)
            movementSpeeds[i] = Random.Range(minSpeed, maxSpeed);
    }

    private void WaypointsAI()
    {
        // We want to iterate over the instances, not over the waypoints.
        for (int i = 0; i < instancesToMove.Length; i++)
        {
            // Check to see if we reached the last waypoint.
            // If so, stop / loop / turn around as needed.
            int waypointIndex = nextWaypointIndices[i];
            if (waypointIndex < 0 || waypointIndex >= waypoints.Length)
            {
                switch (completeBehaviour)
                {
                    case PathCompleteBehaviour.Stop:
                        // Leave it where it is and skip to the next instance.
                        continue;
                    case PathCompleteBehaviour.Loop:
                        // Wrap front to back / back to front.
                        waypointIndex = (waypointIndex + waypoints.Length) % waypoints.Length;
                        break;
                    case PathCompleteBehaviour.Reverse:
                        // Bounce back to the point before last.
                        waypointIndex += (movementSpeeds[i] > 0f) ? -2 : 2;
                        // Reverse direction.
                        movementSpeeds[i] *= -1f;
                        break;
                }
                nextWaypointIndices[i] = waypointIndex;
            }


            // Look up the waypoint we want to move toward next.
            Vector3 waypoint = waypoints[waypointIndex].transform.position;

            // Read this instance's speed,
            // to compute how far it should move in this frame.
            float travel = Mathf.Abs(movementSpeeds[i]) * Time.deltaTime;

            // Apply the motion.
            Vector3 position = instancesToMove[i].transform.position;
            position = Vector3.MoveTowards(position, waypoint, travel);
            instancesToMove[i].transform.position = position;

            // If we're within a configurable tolerance of the
            // destination waypoint, set our destination
            // to the next waypoint after it.
            if ((position - waypoint).sqrMagnitude < 0.1f)
            {
                nextWaypointIndices[i] += (movementSpeeds[i] > 0f) ? 1 : -1;
            }
        }
    }
}
Tags : unity c#


Related Questions


How to animate objects in Unity 3D

Updated July 12, 2015 13:05 PM