• Hey! Guest! The 37th GMC Jam will take place between May 28th, 12:00 UTC and June 1st, 12:00 UTC. Why not join in! Click here to find out more!

Flying Enemy Charge AI

JeanSwamp

Member
Hello,

I'm trying to get some sort of flying enemy charge towards the player. I'm moving the enemy towards the player like this:

Code:
    hsp = lengthdir_x(6,point_direction(x,y,_playerx,_playery))
    vsp = lengthdir_y(6,point_direction(x,y,_playerx,_playery))
Now, what's a good way to stop this on a timer, or when reaching certain point to make the enemy leave this charge state smoothly.

The idea is to allow the player to be able to dodge the charge by moving, since the enemy will charge towards the current player location in the moment the charge began.

Thanks
 
Last edited:

johnwo

Member
At charge start, set playerx/y then just go towards that x/y...

Edit: You changed the question.
Using alarms/point_distance to stop the charge would be the easiest way to go about it.
 

JeanSwamp

Member
Using alarms/point_distance to stop the charge would be the easiest way to go about it.
Problem is I just don't want to stop it based on an alarm, because it won't take in consideration from how far the charge began, so if it was further away, it might not even reach the player.

This is more or less what I'm doing. Is working but I need it to be a bit more smooth and random.

Code:
#region Warning
if e_state = e_states.warning
{  
    sprite_index = sEnemy1_Attack;
    hsp = 0;
    image_speed = 0;

    // THROW ATTACK WHEN THE WARNING IS OVER
    if attackwarning == 0
    {
            image_index = 0;
            e_state = e_states.attack
            startx = x
            starty = y
            targetx = oPlayer.x
            targety = oPlayer.y
    }
}
#endregion

#region Attack
if e_state = e_states.attack
{
    hsp = lengthdir_x(6,point_direction(x,y,targetx,targety));
    vsp = lengthdir_y(6,point_direction(x,y,targetx,targety));
   
    if point_distance(x, y, targetx, targety)  < 8
    or place_meeting(x+hsp,y,oPlayer)
    {
        attackcd = 150;
        e_state = e_states.recovery;
    }
}
#endregion
#region Recovery
if e_state = e_states.recovery
{
    misctimer++
    if misctimer >= 20
    {
        hsp = 0;
        vsp = 0;
        misctimer = 0;
        e_state = e_states.stepback;
    }
}
#endregion
#region Step Back
if e_state = e_states.stepback
{
    hsp = lengthdir_x(2,point_direction(x,y,startx,starty));
    vsp = lengthdir_y(2,point_direction(x,y,startx,starty));
    if point_distance(x, y, startx, starty)  < 8
    {
        hsp = 0;
        vsp = 0;
        e_state = e_states.run;
    }
}
#endregion
I want both the attack reaching it's end to be a bit smoother and fluid, and same for the step back, currently I'm moving it towards the starting pos but I would like to reposition in another different stop to add randomness.
 
Last edited:

johnwo

Member
I want both the attack reaching it's end to be a bit smoother and fluid, and same for the step back, currently I'm moving it towards the starting pos but I would like to reposition in another different stop to add randomness.
What do you mean by "smoother"? Smoother in terms of timing, or smoother as in terms of motion? If it's in terms of motion, do you want the speed to ramp up or down? Do you have any examples/references?
The question is just a bit ambiguous...
 
Top