Needing help preventing enemies from running off the level.

I am using the “Enemy Police Guy” Script from the third person tutorial. I was trying to figure out a way to keep the enemies from literally running off of the level. I tried setting up boundaries to keep them from falling, but that also keeps me from falling off (and since im making a 3d platformer…)
Here is what im looking for:

I was thinking of seeking a vector/Raycast method, but I’m still a novice at that. Any suggestions or examples? The boundary method would be nice, but I don’t know how to keep the player from not being able to move either. Plus, I still want to be able to knock the enemies of (because it’s fun!) I just don’t want them to kill themselves.

Here’s a few ideas to consider:

  1. The optimal solution would probably be some simple pathfinding. Even simple pathfinding can be non-trivial to implement though, so…

  2. You could place colliders marked as triggers around the periphery of the level. Then, when an enemy intersects with a boundary collider, have the enemy reverse direction, or apply a force away from the boundary, or something of that sort. You can probably keep them from falling off the level this way, but they probably won’t always behave intelligently (that’s where pathfinding comes in).

  3. Use an ‘obstacle avoidance’ steering behavior, again using triggers to let the enemy know that an edge is nearby.

There are other variations you could employ as well. As far as achieving the appearance of intelligent behavior goes, steering behaviors might get you pretty close, but pathfinding (e.g. waypoint or navigation-mesh based) would probably be your best bet. (I think AngryAnt from this forum created a pathfinding solution that can be used with Unity, so that might be something to look into.)

SirVictory - you were on the right path with the boundry colliders, the trick is to disable collisions with your player character.

In my game I tag the edge of platform colliders as “invisible” then any character that’s allowed to fall off edges has this in their OnEnable function :

// find 'invisible' physics (representing cavities) and disable collision
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("invisible"); 

for (var go : GameObject in gos)  { 
	Physics.IgnoreCollision(go.collider, collider);
}

You can even switch this on and off as you need (for instance if you want enemies to be able to fall off edges when being thrown by an explosion)

Sycle I put your segment of code in and it works like a charm! (of course after making the edge tags, thanks!)

Now I’m going to have to make a bunch of barriers though for where my enemies will be.

In the end, it just may be better to do raytracing to figure out if the enemy is approaching a drop, and turn them. It’ll save you a bunch of work in the later.

The only problem is that I am not sure how to do that yet in code. :frowning:

Also, I need to figure out how to have the enemies “ignore” the boundaries when they get hit.

Edit: I am now using the waypoint script from the FPS demo. Seems to be working, the enemies follow a path in the waypoints and go off track to come attack me! Im so happy :smile: