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

GameMaker Orbiting Object Jumping Around

T

Tyson Boyer

Guest
I noticed someone posted something very similar to this a day ago, but not quite the same since I have the orbiting part coded. Was not sure if it would be better to piggyback that thread or create my own so hope I made the right decision. Anyways, onto the question.

I want my enemy to strafe around the player, which I have pretty much got it to do, but the enemy object will, usually, but not always, jump to another location once and then begin to strafe. I think it is happening because the enemies x and y are changing between state transitions, but I am not sure. This is what it looks like.

This is the alert state that creates a path for the enemy to follow to the player and when the enemy is close enough it will enter the attack state.
Code:
if (instance_exists(o_player) && collision_circle(x, y, alert_radius, o_player, false, false)) {
    show_debug_message("ALERT");
    mp_grid_path(global.enemy_pathing_grid, global.enemy_path, x, y, o_player.x, o_player.y, true);
    path_start(global.enemy_path, alert_spd, path_action_stop, false);
    
    if (collision_circle(x, y, fight_radius, o_player, false, false)) {
        show_debug_message("FIGHT");
        path_clear_points(global.enemy_path);
        current_state = STATE_ENEMY.ATTACK;   
        previous_state = STATE_ENEMY.ALERT;
    } else {
        current_state = STATE_ENEMY.ALERT;   
    }
} else {
    show_debug_message("EXIT ALERT");
    path_clear_points(global.enemy_path);
    current_state = STATE_ENEMY.IDLE;
}
Here is the attack state.
Code:
if (instance_exists(o_player) && collision_circle(x, y, fight_radius, o_player, false, false)) {
    show_debug_message("STRAFE");
    orbit_angle += orbit_speed;
    orbit_length = point_distance(orbit_target.x, orbit_target.y, x, y);
    x = orbit_target.x + lengthdir_x(orbit_length, orbit_angle);
    y = orbit_target.y + lengthdir_y(orbit_length, orbit_angle);
    
} else {
    show_debug_message("EXIT STRAFE");
    current_state = STATE_ENEMY.ALERT;   
}
And here is the relevant information from the enemy create event.
Code:
alert_radius = 128;
alert_spd = 1.5;

attacking = noone;
fight_radius = 64;
attack_radius = 24;
attack_count = 0;

orbit_target = o_player;
orbit_speed = 1;
orbit_length = point_distance(orbit_target.x, orbit_target.y, x, y);
//orbit_angle = point_direction(x, y, orbit_target.x, orbit_target.y);
orbit_angle = 0;
 
D

Danei

Guest
The orbit_angle needs to be reset every time the attack state begins. Try putting

Code:
orbit_angle = point_direction(x, y, orbit_target.x, orbit_target.y);
in the same block of code where you set current_state to ATTACK.
 
T

Tyson Boyer

Guest
I should have said in the first post that I tried that when I first wrote the code, but the result was this. The gif doesn't really show how fast the orbiting object is moving back and forth. Its seems like it is trying to be in two places at once.
 
Top