Waypoint pathfinding gone terribly wrong.

Hey everyone, i have this script(Below). And its my messy attempt at doing a waypoint pathfinding script without arrays, the problem is, when the enemy gets to the first waypoint, he turns upways, angled himself, and flies away.

var Waypoint1 : GameObject;
var Waypoint2 : GameObject;
var Waypoint3 : GameObject;
var Waypoint4 : GameObject;
var Waypoint5 : GameObject;
var Waypoint6 : GameObject;
var TargetWaypoint : GameObject;

var Counter : int = 1;




function Start () {
	Waypoint1 = GameObject.Find("Waypoint1");
	Waypoint2 = GameObject.Find("Waypoint2");
	Waypoint3 = GameObject.Find("Waypoint3");
	Waypoint4 = GameObject.Find("Waypoint4");
	Waypoint5 = GameObject.Find("Waypoint5");
	Waypoint6 = GameObject.Find("Waypoint6");
	TargetWaypoint = GameObject.Find("Waypoint" + Counter);
}

function Update () {
	
	var toTarget : Vector3 = TargetWaypoint.transform.position - transform.position;
    
 
    var turnRate : float = 200 * Time.deltaTime;
    var lookRotation : Quaternion = Quaternion.LookRotation(toTarget);
    transform.rotation = Quaternion.RotateTowards(transform.rotation, lookRotation, turnRate);
    
    transform.Translate(Vector3.forward * Time.deltaTime);
    
}


function OnCollisionEnter( hit : Collision)
{
	if (hit.gameObject.name == GameObject.Find("Waypoint" + Counter))
	{
		Counter = Counter + 1;
		Debug.Log("We collided");
		
		TargetWaypoint = GameObject.Find("Waypoint" + Counter);
	}




}

I noticed that this code:
if (hit.gameObject.name == GameObject.Find(“Waypoint” + Counter))
{
Counter = Counter + 1;
Debug.Log(“We collided”);

		TargetWaypoint = GameObject.Find("Waypoint" + Counter);
	}

is not executing for some reason. so i figure thats the problem. but i dont know why its not working.

Any help would be nice.

You are missing a RigidBody component on your player object.Add a RigidBody component to the object which you are attaching this script to.Enable ‘IsTrigger’ on all your waypoint object colliders.

Use this (‘OnTriggerEnter’ instead of ‘OnCollisionEnter’) for detecting the way points.

function OnTriggerEnter( hit : Collider)
{

    if (hit.gameObject.name ==TargetWaypoint.name)
    {
       Counter = Counter + 1;
       Debug.Log("We collided");
 
       TargetWaypoint = GameObject.Find("Waypoint" + Counter);
    }
 
}

Tested Working… Cheers… :slight_smile: