Hide and seek

This may be fairly simple but does anyone know how to have it so that the “enemy” will search and monitor a small area seeking the player? Specifically I’m trying to figure out how to have the enemy seek and how to let the player hide in certain areas.

Throw raycasts from the player to all enemies within a distance, if the raycast reaches the enemy the player is found, otherwise the player is hidden. To find all near enemies you can iterate throw all enemies every frame and check the distance, it’s probably not worth it to it otherwise.

Now of course the “enemy” should have some kind of AI to move arround, maybe you can make it follow a path or make it move randomly (or you can do both, have a garph of locations and chose the next node randomly).

Use a navmesh for the enemy that only covers a certain area. If the player is outside of that area, the enemy can’t reach him. Otherwise, he will seek out the player wherever he is on the navmesh.

Seeking with “gaps” is not that simple as you might think of. The problem is that there is no build-in method to “ignore what is behind an obstacle”. This is possible during render but not within logic.

The sensors you have will see the complete sensor range. But there are some characteristics you can use.

A) as mentioned above use one or more ray sensors. The can be blocked by objects, but the measured are is pretty small (a line).
B) near sensor/ radar sensor - they will not be blocked but sense a large area. They eat a lot of processing power.
C) collision sensor - as B) but more efficient

D) combination of the above methods. E.g. use B/C and check if A)

You could also use a track-to-closest object script instead of the navmesh, if you wanted. This will allow the enemy to track to a property, instead of an object, then place nodes (planes) with this property for the enemy to track to, (Player should also have this property)

Use a ray bone parented to the enemies head, or parented to the enemies collison box, looking for the player, animate it so it sweeps back and forth, covering a larger area. A ray won’t see through walls, radar will.

And use states on your enemy.

state 1 = enemy is simply patrolling the area. Either by Navmesh, using a target that will randomly move script. or track-to-closest object script using nodes to track to.

State 2 = enemy has seen the player, or the player is now close enough, and will now chase the player. or use the navemesh to now target the player. You could place a delay on state 2, to change back to state 1, to simulate the enemy losing the player, and going back to patrolling.

There are many options using states. IE run for cover, shoot at player, throw a grenade, etc.