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 () {
}
}