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

Legacy GM making enemy ai jump

F

fxokz

Guest
In my game if an enemy is close enough to you it will go into its chase state in which it moves in your direction.
I want to add a system so that if there is an obstacle in the way OR if there isnt a platform in front it will attempt to jump over the gap. The problem is how can i program the ai so that it can know if there is a platorm where it can jump.. Otherwise it just jumps off a cliff and dies :/

Right now when the enemy ai jumps it just keeps flying in the air off the screen..
Code:
// enemy collision
if (place_meeting(round(x+hsp),round(y),obj_block))
{
    while(!place_meeting(round(x+sign(hsp)),round(y),obj_block)) x += sign(hsp);
    hsp = 0;
} if (!place_meeting(round(x+hsp),round(y+1),obj_block))
{
    vsp = -6;
}
also if there is a box in front of the enemy, how can i make it know if its jumpable or not.
 
Last edited:

Tthecreator

Your Creator!
I'm guessing hsp and vsp stand for horizontal and vertical speed.
Maybe you could add the "if (!place_meeting(round(x+hsp),round(y+1),obj_block))" inside your player for debugging purposes and then draw either "true" or "false" according to the outcome of that if statement.
That way you'll be able to debug it better. Just walks around a bit, jump, stand on the edges of blocks and try to see if your code activates in certain scenarios it shouldn't get activated.

Also isn't there supposed to be any code to stop the enemy from jumping?
What you want should be a code that really makes the enemy jump, not to make it fly.
For this you can take a look at the code inside your player which also probably has some code to jump.
 

NightFrost

Member
For jumping over a gap, you have to make the enemy check there is a gap right ahead and there is a platform where it's jump trajectory would make it land. (Likewise, if there is a gap where it would land, make the enemy stop or turn around so it won't fall). Jumping over an obstacle works the same way. If there is an obstacle ahead, and there is platform where jump would land, execute jump.

Trying to do something more complex than reacting to immediate surroundings starts to need proper platform pathfinding.You can't use GML's motion planning grid for that because its node map is in grid formation, and platform nodes need a freely definable node map.
 
C

Chillbot17

Guest
I got my enemies to jump, but they pogo until they hit a wall above them. After that they stop jumping all together. What I did is add a jump limit variable so they can't fly off into space and added to the place meeting part a reset feature for the variable. I'll post my code:
//Move
vspeedplayer_ +=gravity_;
if place_meeting(x+hspeedplayer_, y, obj_wall)
{
enemyjump_limit_ = 1;
hspeedplayer_ = 0;
}
x +=hspeedplayer_;

if place_meeting(x, y + vspeedplayer_, obj_wall) {vspeedplayer_ = 0; enemyjump_limit_ = 1} else if !place_meeting(x +- 2,y,obj_wall) {
{ if (enemyjump_limit_ = 1){
vspeedplayer_ = enemyjump; enemyjump_limit_ = 0 } else { vspeedplayer_ +=gravity_; } } }
y += vspeedplayer_;
 

TheouAegis

Member
Is there a block in front of my legs? If yes, is there a block in front of my face? If yes, walk up to the block and stop. If there is a block in front of my legs but there is not a block in front of my face, jump on top of the block. If there is not a block in front of my legs but there is a block in front of my face, crawl under the block until there is no longer a block above my head. If there is no no ground at all in front of me, check if there is ground beyond there within the distance that I can jump. If there is, jump with enough V speed and H speed to clear the jump. If there is not, then turn around.

Programming aai is not super difficult. All you are doing Is telling the AI what you would do in the same situation. If you are walking down the street and there is a curb in front of you, do you keep walking off the curb? Or do you stop at the curb and turn around? Neither answer is really correct. What you do is you look beyond the curb and Verify if there is road on the other side of that curb. If there is road then you step off the curb. If there is a giant manhole open on the other side, you turn around. You need to tell the AI the same thing. Is it safe to continue walking forward? The only way to know is if you take in all of your immediate surroundings.
 
A

Ankuran Dreams

Guest
In my game if an enemy is close enough to you it will go into its chase state in which it moves in your direction.
I want to add a system so that if there is an obstacle in the way OR if there isnt a platform in front it will attempt to jump over the gap. The problem is how can i program the ai so that it can know if there is a platorm where it can jump.. Otherwise it just jumps off a cliff and dies :/

Right now when the enemy ai jumps it just keeps flying in the air off the screen..
Code:
// enemy collision
if (place_meeting(round(x+hsp),round(y),obj_block))
{
    while(!place_meeting(round(x+sign(hsp)),round(y),obj_block)) x += sign(hsp);
    hsp = 0;
} if (!place_meeting(round(x+hsp),round(y+1),obj_block))
{
    vsp = -6;
}
also if there is a box in front of the enemy, how can i make it know if its jumpable or not.
I had run into the infinite jumping problem before myself. At one point I had a piece of code written that would limit their jumps to just one use of the jump action until they hit the ground again. I had also found how to prevent the pogo effect so that they only jumped when they were about to collide with the wall. You may just have to make a "jumps" variable that changes when the jumps are used in order to limit their vertical capabilities. It's vague, but it's the thought process I had a few years back when I tried solving this
 
Top