# enemy View Range , chase player

for 3D:

Q1 : I want to make zone range for each enemy in the game , so that when the player in the view range he will be chased .

Q2 :if I want to make enemy walk a little bit around his position ( from -10 to 10 ) for example , i.e as he is searching for some one , can you give me just steps ! thanks

Q3 : How to make enemy moves toward Player if he is being detected ?

Sorry for question , but I am new to this world :)

-
do you want a 2D or 3D solution? – Sidar May 21 '13 at 13:11
sorry Sidar , always forget to post 3D OR 2D :/ – Mohammad Ahmed May 21 '13 at 13:12
3D Sidar , I edit the post :) thanks – Mohammad Ahmed May 21 '13 at 13:15
This looks like a duplicate of another question you asked. – Evan May 21 '13 at 14:06
no , it's not , the previous question was about general thing , but here I want to be more specific , some one told me to ask this in another question ! – Mohammad Ahmed May 21 '13 at 14:27

Disclaimer: This answer is based on the book Artificial Intelligence for Games by Millington and Funge. You should take a look at it for details, and implementations notes at AI4G.com and it's aimed to give readers the general idea trying to avoid copyright infringement.

I'm going to answer in reverse order because I think that's the level of difficulty.

Q3: This is called Seek behavior.

``````Vector3 character, target;
float maxSpeed;

character.velocity = target.position - character.position;
character.velocity.normalize();
character.velocity *= maxSpeed;
if (velocity.lenght() > 0)
character.orientation = atan2(-character.velocity.x, character.velocity.z);
``````

Q2: This is called Wander behavior.

``````float randomBinomial () {
return random() - random();
}

Vector3 character;
float maxSpeed, maxRotation;
// this is the vector that indicates to where you are looking at
// sometimes it is similar to where you are going (velocity), but not necessarily
character.velocity = maxSpeed * character.orientation.asVector();
// rotation is the speed at which you change the orientation per frame
character.rotation = randomBinomial() * maxRotation;
``````

It's important to note that if Wander behavior is not exactly what you're trying to achieve (because you want to be always inside the area), the best option I can offer is to apply the Seek behavior to a random target in the circumference. Once you reach the target (or are pretty close to it), generate a new point. This way you can monitor a given area and avoid getting similar random points inside the circle.

EDIT: You can checkout my implementations of some of those behaviors in XNA here. They are in the GLaDOS class. However, you can checkout the code from the book and translate it from C++ into C# with almost no problem at all. A little bit of patience :)

Q1:

This one is a bit tricky and could be solved differently. You can shoot several rays to where you are looking at given a threshold (cone aperture), ray distance and number of rays (one per degree, maybe). If the ray hits the target, apply seek behavior.

A cheaper way is to shoot the ray and, given the threshold, you check collision detection inside the area of the rectangle (is not that precise, but gives the same idea).

Finally, you can shoot the ray and create two triangles. If the object collides with either one of them, that's it.

EDIT: 3D collision detection is well covered in Stack Overflow here and here. They helped a friend of mine last year.

-
Thaaaanks pctroll !!!!!! , the most great answer I had ever get ! thanks , Ok for the last one (Q1) , can you give general code (3D) about making rectangle and check if I am inside because as you said this is the most cheap ! :) – Mohammad Ahmed May 21 '13 at 16:55
AI4G.com , I tried it before :/ I am working with XNA 4.0 not Directx , by the way can you post the code of seek and wander ( just general steps ) , the code you posted for seek the enemy detect me but not moving toward me but instead moving at his position !! – Mohammad Ahmed May 21 '13 at 21:23
There you go. I hope the edition helps you. Now it's time to check out the code and adapt it to your needs. When I say "character" it could mean anything. If the bot is supposed to seek the player, character=>bot and target=>player. – pctroll May 21 '13 at 21:43