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

How can I make an enemy jump over a block?

D

Den

Guest
So I'm not making any type of path finding thing, just more of a: when an enemy is chasing the player it jumps up to the player or over a obstacle.

I have some code I was trying but I can't get it to work at all, the chase and the collisions work fine but the jump part doesn't and I don't understand why.

Here's all the code involved

The enemy's Step event :
Code:
///Move The Enemy
script_execute(scr_chase_state);

//Apply gravity
if (place_meeting(x, y+1, obj_solid)) {
    vspd += grav;
}

//Horizontall collisions
if (place_meeting(x+hspd, y, obj_solid)) {
    
    hspd = 0;
}

//Move Horizontally
x += hspd

//Virtical collions
if (place_meeting(x, y+vspd, obj_solid)) {
    
    while (!place_meeting(x, y+sign(vspd), obj_solid))
    {
        y+= sign(vspd);
    }

    vspd = 0;
}

//Move virtically
y+= vspd;

The Chase State
Code:
///scr_chase_state()

//Get the distance from the mouse
if (point_distance(x, y, mouse_x, y)>16) {
    if (x < mouse_x)
    {
        hspd = 4;
    }else {
        hspd = -4;
    }
}else {
    hspd = 0;
}

//Set up jump check vars
var on_ground = place_meeting(x, y+1, obj_solid);
var mouse_above = (mouse_y < y);
var wall = place_meeting(x+hspd, y, obj_solid);
var xpos = x+sign(hspd);
var ypos = y+(sprite_height/2)+1;
var ledge = !instance_position(xpos, ypos, obj_solid);

//Check for jump
if (on_ground && mouse_above && (wall || ledge)) {
    vspd = -jspd;
}
 

Bingdom

Googledom
Store a variable called 'jumped'
Whenever there isn't a place_meeting under it and it hasn't jumped, then jump and set 'jumped' to true until it hits the ground again.

Same process for checking for an obstacle in front of it, but you may want to predict a bit further ahead for a smooth jump.
 

TheouAegis

Member
Is there ground in front of me? If no, jump over the hole.
Is there a block in front of my feet? If no, move forward.
Is there a block above that block? If yes, turn around.
Otherwise, jump over the block.
 

Roderick

Member
Is there ground in front of me? If no, jump over the hole.
Is there a block in front of my feet? If no, move forward.
Is there a block above that block? If yes, turn around.
Otherwise, jump over the block.
I would also check if there was ground at the landing location , so your enemies aren't jumping into pits on their own, and one to see if they can reach the ground above, so that they don't stand in a corner jumping at a ledge they can't reach.

If everything's standard sizes, and the enemy can always make every jump, you can skip this step.
 
Top