Prefab and Waypoints

After trials of trying to attach a waypoint to a prefab(that failed miserably). I decided to to research with prefabs and waypoint, now from what I have read. I have to connect the prefab to the first waypoint in order for it to work properly or go to the next waypoint. My problem is, I don’t know how to get the first “enemy” to go to the first waypoint and I am confused on how to make the script.

This is what I attached to the prefab:

using UnityEngine;
using System.Collections;

public class enemyMover : MonoBehaviour {
	
	[SerializeField]
	//private Transform[] waypoint;
	private Waypoint currentWaypoint;
	private Vector3 velocity = Vector3.zero;
	private float thresholdDistance = 0.1f;
	private Waypoint firstWaypoint;
	
	[SerializeField]
	private float speed = 20;
	
	void OnTriggerExit(Collider c)
	{
		Debug.Log (c.gameObject.tag);
		if(c.gameObject.tag == "Bullet")
		{
			speed = 10;
		}
	}
	
	// Use this for initialization
	void Start () {
		//currentWaypoint = 0;
		firstWaypoint = GameObject.FindGameObjectWithTag("Start");
		//I deleted all the fail codes
	}
	
	// Update is called once per frame
	void Update () {
		

			Vector3 target = currentWaypoint.transform.position;
			Vector3 moveDirection = target - transform.position;
			velocity = rigidbody.velocity;
			
			if(moveDirection.magnitude < thresholdDistance)
			{
				currentWaypoint = currentWaypoint.GetNextRoute();
			}
			else
			{
				velocity = moveDirection.normalized * speed;
			}
			
			rigidbody.velocity = velocity;
		}

	
}

What’s inside the waypoints:

using UnityEngine;
using System.Collections;

public class Waypoint : MonoBehaviour {
	
	
	[SerializeField]
	private Waypoint[] nextRoutes;
	[SerializeField]
	private float heuristicPoints = 1.0f;
	
	
	
	public Waypoint GetNextRoute()
	{
		int chosenPathIndex = 0;
		if(nextRoutes.Length == 1)
		{
			return nextRoutes[0];
		}
		else
		{
			for(int i = 1; i < nextRoutes.Length;i++)
			{
				if(nextRoutes[i].heuristicPoints < nextRoutes[chosenPathIndex].heuristicPoints)
				{
					chosenPathIndex = i;
				}
			}
		}
		return nextRoutes[chosenPathIndex];
	}
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	
	}
}

ok, so the waypoints basically consist of an array with all the other way points connected to the one being constructed and a node weight value for path search.

If you’re doing a linear path with only a single connection per waypoint we can skip the node weight bit.

When you create the waypoints, how are you linking each of them together?

If you’re setting up the path manually by dragging the waypoint prefab into the hierarchy, you’ll need to drag waypoint 2 into waypoint 1’s “nextRoutes” array parameter, 3 into 2, 4 into 3 etc.

Dragging the waypoint to a prefab won’t work with me. What I did is, the first waypoint is going to be connected to the second waypoint. That’s how I connect the waypoint to another.

My problem is, I need to make the enemy move to the first waypoint so it can chain to the next waypoint.

oh… you have a “currentWaypoint” and a “firstWaypoint” var on the enemyMover script. firstWaypoint gets set in Start(), but the Update uses currentWaypoint to move to…

If you want the enemies to immediately move along the waypoints when they are spawned you could set the currentWaypoint in Start() instead of firstWaypoint.

If you want the to only move when a certain condition is met you need to expand the update to cover that. (really depends on what you’re making but something like “if (close enough to player) currentWaypoint = firstWaypoint;” would do it)

(and just in case… the first waypoint in your path has the “Start” tag set?)