Legacy GM (Please Don't) Exit stage left. SOLVED!

Null-Z

Member
For my Boss object, I have a phase where it hovers over the player before dropping down. I want it to move back and forth around the player's X before dropping.
the problem?
it just float's away seemingly ignoring the conditions I set for it.

Code:
//Sprites
sprite_index = spr_BearFangs;
//Counts
vspeed = 0
SeekTimer -= 1;
StuckTimer = 120;
//Conditions
if SeekTimer >= -240
{
if instance_exists(obj_Player)
    {
     
        if x > obj_Player.x
                {
                    hspeed -= 0.3
                }
               
           
       if x < obj_Player.x
                {
                    hspeed += 0.3
                }
    }
}

//State Transitions
if instance_exists(obj_Player)
{
    if (SeekTimer <= 0) && (x = obj_Player.x)
        {
        hspeed = 0;
         state = SnakeBoss_state.Drop;
        }
}
if hp <= 0
    {
        state = SnakeBoss_state.death;
    }

What am I misunderstanding?

Functioning code
Code:
//Sprites
sprite_index = spr_BearFangs;
//Counts
vspeed = 0
SeekTimer -= 1;
StuckTimer = 120;
//Conditions


if SeekTimer >= -600
{
if instance_exists(obj_Player)
    {
      
      var flip_distance = 50;

            if (x > obj_Player.x + flip_distance)
                {
                    hspeed -= 0.3;
                    if hspeed <= -5
    {
        hspeed = -5
    }
                    
                }
        else
                if (x < obj_Player.x - flip_distance)
                {
                    hspeed += 0.3;
                    if hspeed >=5
    {
        hspeed = 5
    }
                }
    }
}

//State Transitions
if instance_exists(obj_Player)
{
    if (SeekTimer <= 0) && (abs(x - obj_Player.x) < 2.00)
        {
        hspeed = 0;
         state = SnakeBoss_state.Drop;
        }
}


  
if hp <= 0
    {
        state = SnakeBoss_state.death;
    }
 
Last edited:
R

robproctor83

Guest
Not 100% sure this will fix it, but you have a typo here:

if (SeekTimer <= 0) && (x = obj_Player.x)

It should be == I assume
 

Simon Gust

Member
A problem that you might be seeing is the boss not moving left to right very well. By setting how far the boss should overshoot the player before turning back to it will give you a more polished effect.
What's happening here
Code:
if x > obj_Player.x
{
    hspeed -= 0.3
}
if x < obj_Player.x
{
    hspeed += 0.3
}
is that as soon as the boss is slightly to the left of the player it will start moving towards the right, but because he is so close already, it only takes 1 frame to reach the turning point.

Code:
var flip_distance = 100;

if (x > obj_Player.x + flip_distance) 
{
    hspeed -= 0.3;
}
else
if (x < obj_Player.x - flip_distance) 
{
    hspeed += 0.3;
}
By implementing a flip distance, the boss ensures that he moves a certain distance before turing in towards the player.

Another thing that could cause problems is this
Code:
if (SeekTimer <= 0) && (x = obj_Player.x)
the boss' x and the player's x might not ever reach the same value.
It is better to give a little room for error.
Code:
if (SeekTimer <= 0) && (abs(x - obj_Player.x) < 2.00)
This ensures that the boss drops even if they're up to 2 pixels apart in either left or right direction
 

Null-Z

Member
A problem that you might be seeing is the boss not moving left to right very well. By setting how far the boss should overshoot the player before turning back to it will give you a more polished effect.
good news is, your suggestion did the trick, bad news is the player can just run back and forth and the boss's hspeed just keeps increasing and never drops. a coded hspeed cap should fix this.

Edit: Speedcap worked! reset the room a few times to make sure it behaves itself and it still functions. Fixed code in OP
 
Last edited:

TheouAegis

Member
What happens if SeekTimer is -240? That's an oddly specific negative value...

You could have something like
Code:
if abs(obj_player.x - x) < abs(hspeed) && !SeekTimer
    SeekTimer = -255;
if SeekTimer < -254
    if abs(hspeed) < 0.5 hspeed = 0;
    else hspeed -= 0.5*sign(hspeed);  //I use 0.5 instead of 0.3 for faster slow-down here
 

Null-Z

Member
What happens if SeekTimer is -240? That's an oddly specific negative value...

You could have something like
Code:
if abs(obj_player.x - x) < abs(hspeed) && !SeekTimer
    SeekTimer = -255;
if SeekTimer < -254
    if abs(hspeed) < 0.5 hspeed = 0;
    else hspeed -= 0.5*sign(hspeed);  //I use 0.5 instead of 0.3 for faster slow-down here
the -240 is a result of trying to give the object time to re-align with the player's X. since room's are set to 60fps I use multiples of 6 + a zero on the end time things in seconds.
Exempli Gratia: I want something to last about 10 seconds so 6 * 10 = 60 then tack a zero on the end for 600.
and unfortunately I'm not that high a level in coding knowledge yet.
 
Top