#Stuck Pathfinding
1 messages · Page 1 of 1 (latest)
CREATE EVENT:
`randomize();
pdead = false;
image_xscale = 1;
image_yscale = 1;
velocity = random_range(0.4, 0.65);
initial_velocity = velocity;
spot_velocity = random_range(0.15, 0.3);
hp = irandom_range(2,3);
last_x = x;
last_y = y;
if (!variable_global_exists("enemygrid")) {
var w = room_width div 16;
var h = room_height div 16;
global.enemygrid = mp_grid_create(0, 0, w, h, 16, 16);
mp_grid_add_instances(global.enemygrid, obj_solid, true);
}
path = path_add();
target_x = obj_player.x;
target_y = obj_player.y;
alarm[0] = 1;
p_run = 8;
immortality = false;
memory = 0;
max_memory = irandom_range(200, 300);
wander_target_x = x;
wander_target_y = y;
wander_distance = 80;
rst = irandom_range(20, 45);
spot_time = rst;
role = choose("bruiser", "gunner");
if role == "gunner" {
gun = instance_create_depth(x,y, 1, obj_enemygun);
gun.enemy = id;
}
if role == "bruiser" {
distance = 2;
} else {
distance = 10;
}
stuntimer = 0;`
ALARM 0
`path_delete(path);
path = path_add();
mp_grid_clear_all(global.enemygrid);
mp_grid_add_instances(global.enemygrid, obj_solid, true);
if (memory > 0 && global.hp > 0) {
if role == "bruiser" {
distance = 2;
} else {
distance = 10;
}
if (distance_to_object(obj_player) > distance) {
if (mp_grid_path(global.enemygrid, path, x, y, target_x, target_y, true)) {
path_start(path, velocity, path_action_stop, true);
}
}
} else {
if (point_distance(x, y, wander_target_x, wander_target_y) < 8) && (memory == 0) {
var angle = irandom(359);
var new_x = x + lengthdir_x(wander_distance, angle);
var new_y = y + lengthdir_y(wander_distance, angle);
if (!position_meeting(new_x, new_y, obj_solid)) {
wander_target_x = clamp(new_x, 0, room_width);
wander_target_y = clamp(new_y, 0, room_height);
}
}
if (mp_grid_path(global.enemygrid, path, x, y, wander_target_x, wander_target_y, true)) {
path_start(path, velocity, path_action_stop, true);
}
}
alarm_set(0, 8);`
You can simply cancel starting new path if there are solid objects in the way. collision_line() should do the trick:
if (point_distance(x, y, wander_target_x, wander_target_y) < 8) && (memory == 0) {
var angle = irandom(359);
var new_x = x + lengthdir_x(wander_distance, angle);
var new_y = y + lengthdir_y(wander_distance, angle);
if (collision_line(x, y, new_x, new_y, obj_solid, 0, 0) != noone) {
alarm_set(0, 8);
exit;
}
if (!position_meeting(new_x, new_y, obj_solid)) {
wander_target_x = clamp(new_x, 0, room_width);
wander_target_y = clamp(new_y, 0, room_height);
}
}
You might also want to take a look at mp_potential_path_object() function
from my experience mp_potential_path_object() gets stuck in corners, so the path thing is better in my oppinion
oh i'm late excuse me
Ok, thanks, with this it's already much better, sometimes I don't know why but they still get stuck, but much less often, but now there's another problem, more visual than functional, that is that the enemies go TOO far down. even with the right collisions, in fact even with the mp grid drawn, they overlap.
and sometimes they even flash when they walk, I don't know why, in the sense that either when they go down or up they flip continuously, or it's as if they "vibrate" when they walk
Make sure all of your sprites have their origin in the middle of their collision box. When you mirror sprites via image_xscale = -1, for example, you mirror the collision box as well. This could mess up path collisions, I used to have similar problem in my game.
They have it there
if anyone knows how to fix this, that would be great