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

Making an ememy stop moving when it is near the player

M

MrSanfrinsisco

Guest
The code I have works for the most part however the enemy jitters like crazy if I'm angled a certain way. My character has to be facing a specific way for it to change the sprite index to standing and stop jittering.
Enemy Step Event:
Code:
if (distance_to_object(obj_player) == 100) {
    sprite_index = spr_enemy_standing;
    enemyWalkSpeed = 0;
    move_towards_point(obj_player.x, obj_player.y, enemyWalkSpeed);
} else if (distance_to_object(obj_player) < 100) {
    sprite_index = spr_enemy_walking;
    enemyWalkSpeed = -2;
    move_towards_point(obj_player.x, obj_player.y, enemyWalkSpeed);
} else if (distance_to_object(obj_player) >= 100) {
    sprite_index = spr_enemy_walking;
    enemyWalkSpeed = 2;
    move_towards_point(obj_player.x, obj_player.y, enemyWalkSpeed);
}
 
Code:
var action = 0;
var distancePlayer = distance_to_object(obj_player);
if (distancePlayer == 100) {action = 1;}
if (distancePlayer < 100) {action = 2;}
if (distancePlayer > 100) {action = 3;}

switch(action)
{
case 1:
   sprite_index = spr_enemy_standing;
   enemyWalkSpeed = 0;
break;

case 2:
   sprite_index = spr_enemy_walking;
   enemyWalkSpeed = -2;
break;

case 3:
   sprite_index = spr_enemy_walking;
   enemyWalkSpeed = 2;
break;
}

move_towards_point(obj_player.x, obj_player.y, enemyWalkSpeed);
try that and tellme
 
Last edited:
M

MrSanfrinsisco

Guest
Code:
var action = 0;
var distancePlayer = distance_to_object(obj_player);
if (distancePlayer == 100) {action = 1;}
if (distancePlayer < 100) {action = 2;}
if (distancePlayer > 100) {action = 3;}

switch(action)
{
case 1:
   sprite_index = spr_enemy_standing;
   enemyWalkSpeed = 0;
break;

case 2:
   sprite_index = spr_enemy_walking;
   enemyWalkSpeed = -2;
break;

case 3:
   sprite_index = spr_enemy_walking;
   enemyWalkSpeed = 2;
break;
}

move_towards_point(obj_player.x, obj_player.y, enemyWalkSpeed);
try that and tellme
Okay, I really like how it looks clean so doing things this way might be a good benefit to better coding. However, it still does the same exact thing as the other code.
 

Bentley

Member
The code I have works for the most part however the enemy jitters like crazy if I'm angled a certain way. My character has to be facing a specific way for it to change the sprite index to standing and stop jittering.
Enemy Step Event:
Code:
if (distance_to_object(obj_player) == 100) {
    sprite_index = spr_enemy_standing;
    enemyWalkSpeed = 0;
    move_towards_point(obj_player.x, obj_player.y, enemyWalkSpeed);
} else if (distance_to_object(obj_player) < 100) {
    sprite_index = spr_enemy_walking;
    enemyWalkSpeed = -2;
    move_towards_point(obj_player.x, obj_player.y, enemyWalkSpeed);
} else if (distance_to_object(obj_player) >= 100) {
    sprite_index = spr_enemy_walking;
    enemyWalkSpeed = 2;
    move_towards_point(obj_player.x, obj_player.y, enemyWalkSpeed);
}
Just a guess, but the jitter may be because the numbers for "distance_to_object" are so close together. The instance may be setting one condition to true in one step, moving one way, and then setting another condition to true in the next step, moving another way, and this repeats, causing the jitter.
 
Last edited:
well i dont really know what is your problem, if your problem is that when the object reaches obj_player starts to move like crazy i can explain you how this works.
When you use move_towards_point the computer calculates values in x and y pixels to reach a point based of the speed that you give. That means in this case (in your code) if the object x position(in pixels) is 97 and is trying to reach 100 with a speed of 2, every step the object will add 2 to its x position, so if we add 2 to 97
we have 99 then we add 2 more and we have 101, but in your code you have the stop action when the x value is 100, and that will not happen always as i showed it to you in the example (cause the speed of the move_towards_point depends of an angle (its a trigonometric function) and if u put a speed of 2 that doesnt mean that you will have a 2 pixels movement, sometimes you will have 0 or 1 or 1.32424, etc), thinkingin this way the solution is to use distance_to_object() correctly to calculate if the distance is bigger than your speed.

Code:
var action = 0;
var distancePlayer = distance_to_object(obj_player);

enemyWalkSpeed = 0;

if (distancePlayer > 97 and distancePlayer < 103) {action = 1;}//i put a range where with 2 of speed wil work (but if the speed is bigger  you have to change it again)
if (distancePlayer < 100) {action = 2;}
if (distancePlayer > 100) {action = 3;}

switch(action)
{
case 1:
   sprite_index = spr_enemy_standing;
   enemyWalkSpeed = 0;
break;

case 2:
   sprite_index = spr_enemy_walking;
   enemyWalkSpeed = -2;
break;

case 3:
   sprite_index = spr_enemy_walking;
   enemyWalkSpeed = 2;
break;
}

move_towards_point(obj_player.x, obj_player.y, enemyWalkSpeed);
there is other ways to see if the object has reached a certain position perfect if your speed is bigger but if your speed is 2 or -2 this will work fine
 
M

MrSanfrinsisco

Guest
well i dont really know what is your problem, if your problem is that when the object reaches obj_player starts to move like crazy i can explain you how this works.
When you use move_towards_point the computer calculates values in x and y pixels to reach a point based of the speed that you give. That means in this case (in your code) if the object x position(in pixels) is 97 and is trying to reach 100 with a speed of 2, every step the object will add 2 to its x position, so if we add 2 to 97
we have 99 then we add 2 more and we have 101, but in your code you have the stop action when the x value is 100, and that will not happen always as i showed it to you in the example (cause the speed of the move_towards_point depends of an angle (its a trigonometric function) and if u put a speed of 2 that doesnt mean that you will have a 2 pixels movement, sometimes you will have 0 or 1 or 1.32424, etc), thinkingin this way the solution is to use distance_to_object() correctly to calculate if the distance is bigger than your speed.

Code:
var action = 0;
var distancePlayer = distance_to_object(obj_player);

enemyWalkSpeed = 0;

if (distancePlayer > 97 and distancePlayer < 103) {action = 1;}//i put a range where with 2 of speed wil work (but if the speed is bigger  you have to change it again)
if (distancePlayer < 100) {action = 2;}
if (distancePlayer > 100) {action = 3;}

switch(action)
{
case 1:
   sprite_index = spr_enemy_standing;
   enemyWalkSpeed = 0;
break;

case 2:
   sprite_index = spr_enemy_walking;
   enemyWalkSpeed = -2;
break;

case 3:
   sprite_index = spr_enemy_walking;
   enemyWalkSpeed = 2;
break;
}

move_towards_point(obj_player.x, obj_player.y, enemyWalkSpeed);
there is other ways to see if the object has reached a certain position perfect if your speed is bigger but if your speed is 2 or -2 this will work fine
That did it, thank you!
 
Top