• 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 basic enemy Ai

M

MUSTAFA SALAH

Guest
hello how i can make enemy walk left and right and dont fall from the wall

thanks :)
 

TheouAegis

Member
Set gravity to zero. There, now it won't fall off the wall. That was how Shaun expected people to handle enemies. LOL

Do you mean you want an enemy that moves like a red koopa troopa or like most enemies in old Castlevania games?
 
T

Teknopants

Guest
Hey Mustafa,
One way to do this is to have the enemy check the ground ahead of them to see if there is anything there, and if not, turn them around.

You can use position_meeting() and check at a point ahead of where the character is looking and below the feet of the character.

HorizontalEnemy.gif

When they don't detect any ground, reverse the side they are looking. The side they are looking should also determine where they are checking ahead of them, so this way the moment they reverse, they'll be detecting ground again and will continue on track.

Here's my code for the enemy:

CREATE:
Code:
xspd = 0; //current horizontal speed

maxSpd = 2; //maximum horizontal speed
accel = maxSpd/5; //acceleration. Gets to max speed in 5 frames

side = 1; //side facing. 1 = right, -1 = left
STEP:
Code:
//if you're on top of a wall but there isn't a wall 9 pixels ahead of you, turn around
if place_meeting(x,y+1,Wall) && !position_meeting(x+9*side,y+1,Wall)
{
    side *= -1;
}

//xspd will try to reach maxspd
if xspd < maxSpd*side
    xspd += accel;
if xspd > maxSpd*side
    xspd -= accel;
  
x += xspd;

You can download this project file here: https://www.dropbox.com/s/r4gppgcua1lxhpj/GMH_HorizontalEnemy.yyz?dl=0
 
M

MUSTAFA SALAH

Guest
Set gravity to zero. There, now it won't fall off the wall. That was how Shaun expected people to handle enemies. LOL

Do you mean you want an enemy that moves like a red koopa troopa or like most enemies in old Castlevania games?
Yes i want to make it like koopa.... Thanks
 
M

MUSTAFA SALAH

Guest
Hey Mustafa,
One way to do this is to have the enemy check the ground ahead of them to see if there is anything there, and if not, turn them around.

You can use position_meeting() and check at a point ahead of where the character is looking and below the feet of the character.

View attachment 22423

When they don't detect any ground, reverse the side they are looking. The side they are looking should also determine where they are checking ahead of them, so this way the moment they reverse, they'll be detecting ground again and will continue on track.

Here's my code for the enemy:

CREATE:
Code:
xspd = 0; //current horizontal speed

maxSpd = 2; //maximum horizontal speed
accel = maxSpd/5; //acceleration. Gets to max speed in 5 frames

side = 1; //side facing. 1 = right, -1 = left
STEP:
Code:
//if you're on top of a wall but there isn't a wall 9 pixels ahead of you, turn around
if place_meeting(x,y+1,Wall) && !position_meeting(x+9*side,y+1,Wall)
{
    side *= -1;
}

//xspd will try to reach maxspd
if xspd < maxSpd*side
    xspd += accel;
if xspd > maxSpd*side
    xspd -= accel;
 
x += xspd;

You can download this project file here: https://www.dropbox.com/s/r4gppgcua1lxhpj/GMH_HorizontalEnemy.yyz?dl=0
Thanks:)
 
A

Alan Chan

Guest
Actually @Teknopants are most likely not wrong at all the times. Because we need 2 pointers from X1 and X2 to combine on each others to know when to stop from your own left and right sides instead. Good job Teknopants.
Code:
if xspd < maxSpd*side
    xspd += accel;
if xspd > maxSpd*side
    xspd -= accel;
 
Last edited by a moderator:
Top