Enemy AI Dodge and Follow script

This is a script I wrote that allows the enemy to chase the player while going around objects or through a maze or something. It could be much improved but this is what I has so far. I thought it might be helpful for people having trouble getting an enemy to chase a player while moving around objects.

 var target : Transform;

function Update(){
var right = transform.TransformDirection(Vector3.right);
var left = transform.TransformDirection(Vector3.left);
var fwd = transform.TransformDirection(Vector3.forward);

if (!Physics.Raycast(transform.position, fwd, 20)  !Physics.Raycast(transform.position, right, 40)  !Physics.Raycast(transform.position, left, 40)){

transform.Translate(Vector3.zero);
transform.LookAt(target);
transform.Translate(Vector3.forward * 15 * Time.deltaTime);
}
else if (!Physics.Raycast(transform.position, fwd, 20)) {
transform.Translate(Vector3.zero);
transform.Translate(Vector3.forward * 15 * Time.deltaTime);
}
else if (!Physics.Raycast(transform.position, right, 40)) {
transform.Translate(Vector3.zero);
transform.Rotate(0,25,0);
}
else if (!Physics.Raycast(transform.position, left, 40)) {
transform.Translate(Vector3.zero);
transform.Rotate(0,25,0);
}
}
}

Thanks for the script, it looks quite helpful. But how reliable is it, and what if there are two different ways open?
I think I understand what’s going on here, but wonder why you use the transform.Translate(Vector3.zero); before LookAt and translate again, is it to make the enemy stop a while, change direction and then move on? Will it make much difference from just turning and proceeding, since everything happens so quickly?

Thanks again

If there are two ways open, it will always try going right first, you could add a randimization code in to randomly pick which way, or you could lengthen the raycasts to see walls farther on, I just haven’t added those, because my guy is only trying to move around obstacles like rocks and trees, and not through a maze. The Vector3.zero command, is just to stop all movement before it makes it’s next move, I have no idea if it should be in there or if it needs to be in there (most likely it doesn’t need to be in there, but I haven’t tested it).

thanks for the code. Looks like something that would be useful in a boids type simulator.

Yeah ok, sounds good.

I have NPCs that are supposed to climb/jump over obstacles while chasing the player. I thought I could extend your script and let the NPCs start using waypoints on obstacles, when coming close enough, what do you think about that?
Is there a more convenient way to make them climb?

Well, when they get to a place where they can climb or jump, you could just have them use Vector3.up for going up, then just make jumping faster and play the jump animation and climbing slower and play the climbing animation, and depending on the item you can tag it “jump” or “climb” so that the enemies can only jump or climb those by checking for that tag.

I dont know how to use it!!

I am creating a pac man game…

i dont know how to make the ghost go after the player…

I tried alot of things, google, tutorials, even the path on the site!!

But i dont find any tutorial.

Can someone help me find a good tutorial??

look my game http://newwayband.sites.uol.com.br/teste1PacMan.html

thanx!! ^^

wow I think the game looks great, it’s already fun to play :smile:

My suggestion is to make the colliders on the grass a bit smaller, så that the player can have a smooth turn around the corners.

For the raycasting, what’s not working? My advice is to debug as much as you can so that you know exactly where in the code it’s not working (note that debugging makes the frame rate drop A LOT, so once you’re certain something works, remove the debug message).
One problem may be that your range is too long, so the ghosts won’t even have the chance to move before they “hit” a wall. First, make sure they chase the player without any raycasting. When that works, put one raycast and test how far it should raycast. If you’re not sure what the variables in Astrauk’s code are for, check the script reference ( Unity - Scripting API: )

Hope this was helpful
/Kweiko

so I tried you code and it works, kinda of, yeah the object will go to the targeted object, but it just runs right through walls. both the object and the wall have rigidbody attached to them. maybe I’m reading something wrong

This type of AI script isn’t going to help your particular situation much. What it sounds like you need is a waypoint system where when the ghost can either randomly choose waypoints until the pacman target is seen, or choose waypoints based on proximity toward pacman type player. Google for Unity3d Waypointmaster, I’m sure you’ll find something useful.

Good luck :slight_smile:

No, you’re not. transform.Translate does not discern itself with collision. You either need to look into a different method, such as using your rigidbody to perform the movement, or adding in collision checks yourself.

thanks for the input, as soon as I posted my question I caught the problem and your right, but you knew that already. So did something else works just fine now

What kind of Script?