# How to make a vision cone for the enemy in game maker?

I'm making an enemy object where the enemy look for the player when he's going on a patrol

I want the vision cone to changes direction corresponding to the sprite.When the sprite turn right,the cone will faces right,left and the cone will faces left and when he move to any direction the cone will move with him and changes direction accordingly

I also want to give the cone certain ranges.The longest range's where the enemy will be in doubt and get to that position to investigate,medium range he'll start chasing,shortest range he'll attack.

I hope someone can help me.

I will be giving the answer assuming that your sprites were designed this way (with the front side up):

The origin of sprites is middle center and bottom center, respectively.

obj_Enemy - Create Event:

cone=instance_create_depth(x,y,-5,obj_Cone);
cone.owner=self();


obj_Enemy - Step Event:

direction=point_direction(x,y,mouse_x,mouse_y)-90;
seno=dsin(point_direction(x,y,mouse_x,mouse_y));
cose=dcos(point_direction(x,y,mouse_x,mouse_y));

• -90 is for the values ​​to match. It may vary according to the way your sprites were designed.

obj_Cone - Draw Event:

draw_sprite_ext(spr_Cone,0,owner.x+(20*owner.cose),owner.y+(-20*owner.seno),1,1,owner.direction,c_white,1);

• 20 was a value that I stipulated as suitable for my tests, it may be the value that you want in your game.

obj_Cone - Collision with Player Event:

direction=point_direction(x,y,obj_Player.x,obj_Player.y); //For the enemy to turn to the player
...
...


At range/distance you can use the following code in the enemy's step event:

if(distance_to_object(obj_Player)<=300){

}else{

}


But if you prefer you can do something like this, for different distances:

See above, that there are 3 different cones, with green being the smallest and red being the largest. That is, there would be 3 cone objects. You would do the same process for everyone.

For the player not to notice this, they just need to be the same color.

enemy line of sight has some standards that works in all engines and languages. i show you the algorithm,you can implement this on your game maker engine:

if (target overlapping sphere)// if its near than a value to me
{

direction = Normalize(target - this);//target is enemy position. this is my position
dot = Dot(direction, forward);// it means dot product. you can calculate it yourself or find library to do this for you
if (dot > 0.707)// you can change this for bigger or less eye sight
{
//  I see target!
}
}

• Thanks for the help.My vision cone doesn't so good but i'll manage.I also want to know sprite based direction,for example if my sprite index is 'Right' the cone'll turn right and so on.Any suggestions ? – TankRocket Oct 27 '19 at 0:38