Basic waypoint AI

Hi guys. Here i’ve found a simple waypoint ai at youtube. That attacks a player when close enough.

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

function Avake(){
	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;
		var distFromPlayer : Vector3 = player.position - transform.position; 

		if(moveDirection.magnitude < 1){
		currentWaypoint++;
}
else if(distFromPlayer.magnitude < 15){
velocity = Vector3.zero;
target = player.position;
velocity =  (player.position - transform.position).normalized * speed;
if((player.position - waypoint[currentWaypoint].position).magnitude >15){
target = waypoint[currentWaypoint].position;
velocity = moveDirection.normalized * speed;
}
}
else{
	velocity = moveDirection.normalized * speed;
	}
}
else{
if(loop){
currentWaypoint=0;
	}
	else{
		velocity=Vector3.zero;
	}
}

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

The only problem here is that i’ve got no idea how to prevent my “enemy” from moving vertically (on y axis).
If any ideas please share.

you can try something like this

...

var moveDirection : Vector3 = target-transform.position; 
moveDirection.y = 0;

...

Unfortunately it didn’t help :cry:

any ideas?

I posted an example that does what you want on this link http://forum.unity3d.com//viewtopic.php?t=56264&highlight=finite+state+machine+project. It´s in C# and it uses an AI technique I was interested in implementing, but if you look at the ChasePlayer and FollowPath classes you´ll have what you need

Thanx alot! I’m going to digg it tonight.

Finally i’m back from vacation.
dart i’ve tested your script but it has the same “disease” - enemy is fallowing player in y axis. It looks as if enemy is levitating in air…

Are your waypoints all at the same level (ie, the Y coordinate is the same for all of them)?

Yes all my Ys are all on the same lvl.

To not move on the Y axis you have to send 0 for the y value of whatever movement operation you’re performing.

Since you’re using a rigidbody, look for the place where you’re actually changing its velocity.

Right before

rigidbody.velocity = velocity

you have to not move in the y axis.

velocity.y = 0

my variant was

if(player.transform.position.y > 5){
		currentWaypoint++;

but both of them are quite rough…
Here i’ve found a stuff

transform.rotation.eulerAngles = Quaternion.eulerAngles(Quaternion.Slerp().eulerAngles.x, 0, Quaternion.Slerp().eulerAngles .z);

that can do the trick, but i’m not a friend with Slerps and Quaternions, so if anybody can tell me how to implement this snippet into the code, i’ll be very thankful

any ideas?

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

function Avake(){
   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;
      var distFromPlayer : Vector3 = player.position - transform.position;

      if(moveDirection.magnitude < 1){
      currentWaypoint++;
}
else if(distFromPlayer.magnitude < 15){
velocity = Vector3.zero;
target = player.position;
velocity =  (player.position - transform.position).normalized * speed;
if((player.position - waypoint[currentWaypoint].position).magnitude >15){
target = waypoint[currentWaypoint].position;
velocity = moveDirection.normalized * speed;
}
}
else{
   velocity = moveDirection.normalized * speed;
   }
}
else{
if(loop){
currentWaypoint=0;
   }
   else{
      velocity=Vector3.zero;
   }
}

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

velocity.y = 0

What do you mean by ‘rough,’ and how does changing the waypoint when Y is large help to stop vertical movement?

ok, so when my character is higher than enemy, enemy rotates toward the character around x axis, nearly in parallel to the ground…

xD

That’s rotation, not movement. :slight_smile:

replace

transform.LookAt(target)

with

var lookDirection : Vector3 = target.transform.position - transform.position;
lookDirection.y = 0;

transform.rotation = Quaternion.LookRotation( lookDirection );

This’ll rotate the character to face the target, but it’ll ignore any vertical distance between them.

Sorry for bad explanation :sweat_smile:

Ok, according to your suggestion

var lookDirection : Vector3 = target.transform.position - transform.position;
lookDirection.y = 0;
transform.rotation = Quaternion.LookRotation( lookDirection );

it gives me an error:

'transform' is not a member of 'UnityEngine.Vector3'.

When i replace target.transform.position with player.transform.position i see that it does what i want but enemy is allways facing the character.
i just can’t figure out what is the problem with target.

Does anybody have an idea?

Target’s already a Vector3; instead of adding .transform.position, just use “target - transform.position.”

that works perfectly… thanx :frowning: