TransWikia.com

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

Game Development Asked by TankRocket on November 2, 2021

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.

2 Answers

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

enter image description here

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
...
rest of your code
...

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:

enter image description here

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.

Answered by Boneco Sinforoso on November 2, 2021

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!
    }
}

Answered by virtouso on November 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP