CheckPoint Script For Tower Defence Game

I have some questions about making a script that makes my mobs move to a checkpoint and on to the next one.

I really got a problem, i dont know what to write, and i dont really know if its easyer to just make it in my EnemyAI, or if i should make a new one.

Can some one please help me, telling me what to do, or maybe post a script that can help me a bit.

Ill post my EnemyAI, to let u guys see it its easy to put the checkpoints in there or make a new one.

var target : Transform;

var moveSpeed : float = 5.0;

var rotationSpeed : float = 2.0;

var minDistance : float = 2.0;

var maxDistance : float = 30.0;



function Start () {

	var go : GameObject = GameObject.FindGameObjectWithTag("Player");

	target = go.transform;

	minDistance = 2;

}



function Update () {

	CheckGround();

	// check to see if we are out of bounds to move

	if((target.position - transform.position).magnitude < minDistance) return;

	if((target.position - transform.position).magnitude > maxDistance) return;

	

	// hold current rotation

	var rotation = transform.rotation;

	// set the look rotation

	transform.LookAt(target);

	// make sure that the X value in the euler is set to zero.

	transform.localEulerAngles.x = 0; // so we dont end up looking up or down

	// Slerp from the old rotation to the new rotation

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

	

	// get the direction to target

	var direction = target.position - transform.position;

	// check the direction to see if it is forward of the ai

	var checkForward = Vector3.Dot(transform.forward, direction.normalized);

	

	// if we are close to facing in the right direction move forward

	if(checkForward > 0.4) transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);

}



function CheckGround(){

	//check our grounding

	var ray = new Ray(transform.position + Vector3.up * 4.0, -Vector3.up);

	var hits : RaycastHit[];

	hits = Physics.RaycastAll(ray, 2.1);

	var didHit = false;

	for(var hit : RaycastHit in hits){

		if(hit.transform.root != transform.root){

			didHit = true;

			transform.position = hit.point;

		}

	}

	

	// if we did not hit, we need to fall

	if(! didHit){

		transform.position += Physics.gravity * Time.deltaTime;
	}

}

This is actually quite simple to implement. You first need a list of transforms that define your path. Set the target to the first in the list. Every time:

(target.position - transform.position).magnitude < minDistance

set the target to the next waypoint. continue on. Once your waypoint number equals your list length he is done.

But what if i have 4 different ways? :slight_smile:

How do u make the ‘list’ of waypoints you’re talking about??

I’ve got a new prob. for now, i’ve made the list, and not the only problem is, that it only moves to the player, and not to the waypoints. :S

Does i have to make a new script for it, or can i just use my enemyAI??

My EnemyAI is here:

var waypoints : Transform[] = new Transform[2];

var target : Transform;

var moveSpeed : float = 5.0;

var rotationSpeed : float = 2.0;

var minDistance : float = 2.0;

var maxDistance : float = 30.0;



function Start () {

	for (var n=2; n < waypoints.Length; ++n) {

		waypoints[n] = GameObject.Find("p"+(n+1)).transform;

	}

	var go : GameObject = GameObject.FindGameObjectWithTag("Player");

	target = go.transform;

	minDistance = 2;

}



function Update () {

	CheckGround();

	// check to see if we are out of bounds to move

	if ((waypoints.position - transform.position).magnitude < minDistance) return;

	if((target.position - transform.position).magnitude < minDistance) return;

	if((target.position - transform.position).magnitude > maxDistance) return;

	

	// hold current rotation

	var rotation = transform.rotation;

	// set the look rotation

	transform.LookAt(target);

	// make sure that the X value in the euler is set to zero.

	transform.localEulerAngles.x = 0; // so we dont end up looking up or down

	// Slerp from the old rotation to the new rotation

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

	

	// get the direction to target

	var direction = target.position - transform.position;

	// check the direction to see if it is forward of the ai

	var checkForward = Vector3.Dot(transform.forward, direction.normalized);

	

	// if we are close to facing in the right direction move forward

	if(checkForward > 0.4) transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);

}



function CheckGround(){

	//check our grounding

	var ray = new Ray(transform.position + Vector3.up * 4.0, -Vector3.up);

	var hits : RaycastHit[];

	hits = Physics.RaycastAll(ray, 2.1);

	var didHit = false;

	for(var hit : RaycastHit in hits){

		if(hit.transform.root != transform.root){

			didHit = true;

			transform.position = hit.point;

		}

	}

	

	// if we did not hit, we need to fall

	if(! didHit){

		transform.position += Physics.gravity * Time.deltaTime;

	}

}

Thanks,
Jonas :slight_smile: