help with way point system

i’m trying to get an enemy ai to go from point to point in a way point system.

i’ve tried two different way’s and neither of them have really worked out well enough for me so far. i’m trying to push the enemy around as well via force and rigid body physics.

can anyone help with the code to get it to work properly.

Hi, this is probably a very simplistic approach but worked fine for me. Would be great if someone with better programming skills helps to refine this…
Hope it helps
Juan

private var hasReachedLastWaypoint : boolean = false;
var minimunDistance = 7;
//just some game objects - you need only the transform position information
var waypointPath : Transform[];

while(true)
	{
		if(someCondition)//this is necesary only if you want some condition to trigger the following path behavior
		{
			FollowWaypointPath(waypointPath);
		}
		yield;
	}
	
}


function FollowWaypointPath(waypointArray : Transform[])
{
	var curWaypoint : int = 0;
	StartCoroutine("Approach",waypointArray[curWaypoint]);
	while(true)
	{
		if(Vector3.Distance(transform.position,waypointArray[curWaypoint].position) < 0.5)
		{
			
			StopCoroutine("Approach");
			if(curWaypoint < waypointArray.length -1)
			{
				curWaypoint++;
				hasReachedLastWaypoint = false;
				StartCoroutine("Approach",waypointArray[curWaypoint]);
			} else {
				StopCoroutine("Approach");
				hasReachedLastWaypoint = true;
			}
		}
		yield;
	}	
}

function Approach(theTarget : Transform)
{
	while(true)
	{
		if(Vector3.Distance(transform.position,theTarget.position) < minimunDistance)
		{
			var direction = theTarget.position - transform.position;
			transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
			transform.eulerAngles = Vector3(0,transform.eulerAngles.y,0);
			var forward = transform.TransformDirection(Vector3.forward);
			direction = forward * walkingSpeed;
			GetComponent(CharacterController).SimpleMove(direction);
			animation.Play("walk");
		}
		yield;
	}
}

107788–4126–$followpath_850.js (1.49 KB)

took some quite a bit of tweaking but it works very well thank you =)

i did want to use rigid body physics, i’m not quite sure how i will adapt to losing it but it should still be possible.

Glad to help :smile:
I am not very keen on moving physic based agents, but at least theoretically it should work using forces instead of transform based movement…

the FPS tutorial has a waypoint system in it you could check out maybe it would help you with the coding of yours…:slight_smile:

If anyone is interested I modified the FPS tutorial AI some and have the files here: RaidensGamePlace

-Raiden

Is a “path” like all the way points in a empty gameObject.

And dose the empty gameObject need to be a child of were this script is applied to?

Try this out. Very basic way point system.

var waypoint : Transform[ ];
var speed : float = 20;
private var currentWaypoint : int;
var loop : boolean = true;

function Awake(){
waypoint[0] = transform;
}

function Update (){
if(currentWaypoint < waypoint.length){
var target : Vector3 = waypoint[currentWaypoint].position;
var moveDirection : Vector3 = target - transform.position;

var velocity = rigidbody.velocity;
if(moveDirection.magnitude < 1){
currentWaypoint++;
}
else{
velocity = moveDirection.normalized * speed;
}
}
else{
if(loop){
currentWaypoint=0;
}
else{
velocity = Vector3.zero;
}
}

rigidbody.velocity = velocity;
transform.LookAt(target);
}

just add all your waypoint (gameObjects) into the waypoint slot. The thing this script is attached to well fallow the waypoints. You can mess with some var for lurp, rotation and dam. Make the transition from way point to way point more easy.