Posho
Member
Hello,
I am trying to make an enemy move to a random position within a radius. First the position is set and then it travels in the Step Event. It has to move to a position that is longer than a minimum value and shorter than a maximum value. This is how I went with it:
Define target position:
^ First it sets a random angle and keeps rotating adding 45° until an "obj_crate" isn't in the way of the minimum range, checked using a raycast_check function.
Then it sets a random value between the minimum and maximum ranges and sets a random point towards that direction. And finally, using a raycast_coord function defines a point where the instance would collide with "obj_crate".
Step Event:
^ Here, the instance just moves towards the point.
Raycast Check
Raycast Coord
The code works fine and can walk towards walls without getting stuck, but in some random occasions it will get stuck to infinity during the do/until loop. I assumed it's because it got stuck within the "object_crate" so I tried pushing the instance a bit until it's outside but that still doesn't do the trick.
Any ideas? I've been at this for some hours and I just don't see the problem. Help?
I am trying to make an enemy move to a random position within a radius. First the position is set and then it travels in the Step Event. It has to move to a position that is longer than a minimum value and shorter than a maximum value. This is how I went with it:
Define target position:
Code:
var dir = irandom_range(0, 360);
do // First check: the inner circle
{
dir += 45;
}
until (!raycast_check(x, y, dist_min, dist_min, obj_crate, 1))
var dist = irandom_range(dist_min, dist_max);
var try_x = x + lengthdir_x(dist, dir);
var try_y = y + lengthdir_y(dist, dir);
var coords = raycast_coord(x, y, try_x, try_y, obj_crate, 1);
target_x = coords[coord.x_];
target_y = coords[coord.y_];
Then it sets a random value between the minimum and maximum ranges and sets a random point towards that direction. And finally, using a raycast_coord function defines a point where the instance would collide with "obj_crate".
Step Event:
Code:
var add = spd * global.delta;
var dist = point_distance(x, y, target_x, target_y);
if (dist < add)
{
add -= dist;
action = actions.idle;
}
x += lengthdir_x(add, direction);
y += lengthdir_y(add, direction);
Raycast Check
Code:
///raycast_check(x1, y1, x2, y2, collision_object, quality)
var x1 = argument0;
var y1 = argument1;
var x2 = argument2;
var y2 = argument3;
var obj = argument4;
var qua = argument5; // 1 is most precise, but slowest
var found = false;
var dir = point_direction(x1, y1, x2, y2);
var count=0
while (point_distance(x1, y1, x2, y2) > 1)
{
if (collision_point(x1, y1, obj, false, true))
return true;
x1 += lengthdir_x(qua, dir);
y1 += lengthdir_y(qua, dir);
}
return false;
Raycast Coord
Code:
///raycast_coord(x1, y1, x2, y2, collision_object, quality)
enum coord { x_, y_, z_ };
var x1 = argument0;
var y1 = argument1;
var x2 = argument2;
var y2 = argument3;
var obj = argument4;
var qua = argument5; // 1 is most precise, but slowest
var found = false;
var dir = point_direction(x1, y1, x2, y2);
while (point_distance(x1, y1, x2, y2) >= 1)
{
if (collision_point(x1, y1, obj, false, true))
break;
var xp = x1;
var yp = y1;
x1 += lengthdir_x(qua, dir);
y1 += lengthdir_y(qua, dir);
}
var pos;
pos[coord.x_] = xp;
pos[coord.y_] = yp;
return pos;
The code works fine and can walk towards walls without getting stuck, but in some random occasions it will get stuck to infinity during the do/until loop. I assumed it's because it got stuck within the "object_crate" so I tried pushing the instance a bit until it's outside but that still doesn't do the trick.
Any ideas? I've been at this for some hours and I just don't see the problem. Help?
Last edited: