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

Windows Enemies speed out of the level when using "move_towards_point" command

N

nired360

Guest
Hi everyone , am having a problem with my enemy chase ai, i've used the tile collision code from heart beast's tile collision video and it's been working very well, i copied the player movement code and used it on my enemy object (took out the keyboard inputs ), but whenever i try to code in the chase ai , the enemy speeds out of the level no matter how much his speed is set to in "the move_towards_point" command

here is the tile collision code for my enemy object :
// create event
velocity_ = [0, 0];
gravity_ = 1.5;
max_velocity_ = [8, 32];
var layer_id = layer_get_id("collis");
collision_tile_map_id_ = layer_tilemap_get_id(layer_id);

// step event

var vector2_x = 0;
var vector2_y = 1;

//hmovement
velocity_[vector2_x] = clamp(velocity_[vector2_x], -max_velocity_[vector2_x], max_velocity_[vector2_x]);
//fr

//gravity
velocity_[vector2_y] += gravity_;
//moveand contacttiles
move_and_contact_tiles(collision_tile_map_id_, 64, velocity_);
veclocity_[vector2_x] = clamp(velocity_[vector2_x], -max_velocity_[vector2_x], max_velocity_[vector2_x]);
//jumping
var on_ground = tile_collide_at_points(collision_tile_map_id_, [bbox_left, bbox_bottom], [bbox_right-1, bbox_bottom]);

and here's the simple chase ai i used

//step event
if distance_to_object(play) < 40
{
direction = (move_towards_point(play.x,play.y,2));
}
 
N

nired360

Guest
The function move_towards_point does not return any value, so why are you attempting to set that into the direction? I suspect that with the direction being set to a weird value, the object then just goes heading off in that direction and never coming back.

https://docs2.yoyogames.com/index.html?page=source/_build/3_scripting/4_gml_reference/movement and collisions/movement/move_towards_point.html
i tried another method like this
if distance_to_object(play) < 40
{
move_towards_point sign(play.x,play.y,2);
}
else speed = 0;
but the enemy moves away from my character and stops
 
this:
move_towards_point sign(play.x,play.y,2);

should be this:
move_towards_point (play.x,play.y,2);

I don't know where you got sign from, but it's not used here. sign is used to get whether a value is positive / negative / zero. move_towards_point will go wherever it needs to, and there's nothing to add to that - whether the direction is positive or negative it doesn't matter. The command automatically takes care of it.

You only need to put in a check to stop it moving once it's reached that point - generally what you'll see happening is it overshooting the point it's aiming at, and then it constantly to and fro's without settling. Say it was moving 2 pixels, and was 10 pixels away: it would stop on the desired point, as ten is dividable by two. If it was 9 pixels away, and moving 2 pixels, it would keep overshooting as it would always be 1 pixel out
 

CMAllen

Member
if distance_to_object(play) < 40
{
move_towards_point sign(play.x,play.y,2);
}
else speed = 0;
If this is supposed to be a chase function, you're also looking at if the distance to the target is less than 40 before any movement begins. However, if the distance to the target is greater than or equal to 40, no movement happens at all. Is that what you intend? For reference, 40px is a rather small amount. The average sprite is only 32px in width. Do you intend for the chase AI to only function if the player gets right next to something? Or did you mean for it to chase the player but only until it's right next to the player? Because then you'd want to test if the distance is greater than 40.
 
N

nired360

Guest
If this is supposed to be a chase function, you're also looking at if the distance to the target is less than 40 before any movement begins. However, if the distance to the target is greater than or equal to 40, no movement happens at all. Is that what you intend? For reference, 40px is a rather small amount. The average sprite is only 32px in width. Do you intend for the chase AI to only function if the player gets right next to something? Or did you mean for it to chase the player but only until it's right next to the player? Because then you'd want to test if the distance is greater than 40.
yes it is a chase function, am trying to get the enemy object to kinda "spot" the player and when it gets close to him it chases him, i used 40 just for testing but the enemy seems to go away form my player character not chasing him, (and btw i used the sign function
by mistake lol).
 
Top