M
MUSTAFA SALAH
Guest
hello how i can make enemy walk left and right and dont fall from the wall
thanks
thanks
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
//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;
Yes i want to make it like koopa.... ThanksSet 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?
ThanksHey 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:
STEP: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
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
Thanksif !instance_position(x+sign(hspeed)*sprite_width/2, bbox_bottom+1, obj_ground)
{
image_xscale *= -1;
hspeed *= -1;
}
if xspd < maxSpd*side
xspd += accel;
if xspd > maxSpd*side
xspd -= accel;