• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

[SOLVED] AI Pathfinding goes off the level and through walls

D

dannyisfree

Guest
Hey,

I've been trying to create a Hotline Miami clone as a learning experience, but I keep running into the same problem with the AI. The enemies keep going off the level and through the walls after the player gets near the edges of the level.

obj_ grid code:

Code:
//Create the grid
var cell_width = 32
var cell_height = 32

var hcells = room_width div cell_width
var vcells = room_height div cell_height

global.grid = mp_grid_create(0,0,hcells,vcells,cell_width,cell_height)

//add the walls
mp_grid_add_instances(global.grid,obj_wall,true)
obj_enemy add path code:
Code:
path = path_add();
obj_enemy sight code:

Code:
spot_dir = point_direction(x,y,obj_player.x,obj_player.y) //view line check direction

target=collision_line(x,y,x+lengthdir_x(range,spot_dir),y+lengthdir_y(range,spot_dir),obj_player,true,true) //checks for player
if instance_exists(target){
    if !collision_line(x,y,target.x,target.y,obj_wall,true,true){ //checks if theres a wall
        chase = true
        seen=true; //if theres no wall, the player is seen
        
    }else{
        seen=false; //else, the player is not seen
    }

}
obj_enemy follow path code:

Code:
///AI
var plr_x = (obj_player.x div 32)*32+16
var plr_y = (obj_player.y div 32)*32+16


if !seen && chase {
    mp_grid_path(global.grid,path,x,y,plr_x,plr_y,1)
    path_start(path,spd,path_action_stop,false)
}
The enemies create a straight line with the player and checks if there's any walls between them, if there isn't a wall, the enemy will face their direction in order to shoot at the player. Once the player is out of the line of sight after being seen, the enemy will go into chase mode where it uses path finding to get to the player. Once it sees the player again, it stops in its tracks to shoot at the player.

Any help is appreciated, thanks.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
You start them along the path, but where do you stop them? You should call a path_end() function somewhere so that when the instance can't find a path the path is ended (or under whatever other conditions you require).
 
D

dannyisfree

Guest
Turns out all I had to do was set the origin of obj_wall at (0,0). Thanks to everyone who tried to help!
 
Top