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

GMS2 Platformer Enemy Movement

Z

Zeathus

Guest
I want to make a 2D Platformer game but i just can't find a working tutorial on enemy movement. I am a total begginer with programming and if someone could help i would like to thank you!
 
F

FuRyvok

Guest
Hello, how do you want? Like in Mario? When an enemy hits the wall, it changes direction?
 

Bentley

Member
What kind of enemy movement? When I think of enemy movement on a platform, I think of the Koopas in Super Mario World. They go back and forth. I would check a position ahead of you and below you to see if you would walk off the platform. If you would, turn around. You can use the bbox_functions. For example, to check below the koopa, check bbox_bottom + 1. To check ahead, you could also use the bbox_functions: bbox_right + 1 if the koopa is moving right, bbox_left - 1 if the koopa is moving left. image_xscale will be really helpful as you can flip the koopa back and forth.
 
Last edited:
F

FuRyvok

Guest
On the ememy create event add
Code:
hsp = 0;
speed = 3;
//1 will be right and -1 will be left
direction = 1;
Than you will need to make a wall object

This will be a pixel perfect collision with wall

In the enemy step event add
Code:
//direction, speed
hsp = direction * speed;

//collision with wall
if(place_meeting(x+hsp,y,oWall){
while(!place_meeting(x+sign(hsp),y,oWall){
x = x + sign(hsp);
}
direction = direction * -1;
}

x = x + hsp;
 
Z

Zeathus

Guest
What kind of enemy movement? When I think of enemy movement on a platform, I think of the Koopas in Super Mario World. They go back and forth. I would check a position ahead of you and below you to see if you would walk off the platform. If you would, turn around. You can use the bbox_functions. For example, to check below the koopa, check bbox_bottom + 1. To check ahead, you could also use the bbox_functions: bbox_right + 1 if the koopa is moving right, bbox_left - 1 if the koopa is moving left. image_xscale will be really helpful as you can flip the koopa back and forth.

Edit: unhelpful example code.
Yea like the koopas from Super Mario World.
 
Z

Zeathus

Guest
On the ememy create event add
Code:
hsp = 0;
speed = 3;
//1 will be right and -1 will be left
direction = 1;
Than you will need to make a wall object

This will be a pixel perfect collision with wall

In the enemy step event add
Code:
//direction, speed
hsp = direction * speed;

//collision with wall
if(place_meeting(x+hsp,y,oWall){
while(!place_meeting(x+sign(hsp),y,oWall){
x = x + sign(hsp);
}
direction = direction * -1;
}

x = x + hsp;
The walking worked but when he hit the wall the direction didn't change he just walked off!
 
Z

Zeathus

Guest
Hello, how do you want? Like in Mario? When an enemy hits the wall, it changes direction?
I want like when he comes to a edge that he changes the direction. Like the koopas in Super Mario World!
 
Z

Zeathus

Guest
And if it's possible when i jump on his head that he dies! Thanks :D!!!
 

Bentley

Member
On the ememy create event add
Code:
hsp = 0;
speed = 3;
//1 will be right and -1 will be left
direction = 1;
Than you will need to make a wall object

This will be a pixel perfect collision with wall

In the enemy step event add
Code:
//direction, speed
hsp = direction * speed;

//collision with wall
if(place_meeting(x+hsp,y,oWall){
while(!place_meeting(x+sign(hsp),y,oWall){
x = x + sign(hsp);
}
direction = direction * -1;
}

x = x + hsp;
You are setting "speed" in the create event. You are moving the player twice. He will move "speed" in "direction" and "x + hsp". If you want to use the name without using the built-function, just change it to Speed or move_speed or something.

I think you also need to set "hsp" to 0 after moving him against the wall.
 
Last edited:
Z

Zeathus

Guest
You are setting "speed" in the create event. You are moving the player twice. He will move "speed" in "direction" and "x + hsp" in the loop. If you want to use the name without using the built-function, just change it to Speed or move_speed or something.

I think you also need to set "hsp" to 0 in that loop. You move him against the wall, but keep "hsp" which means he will move into the wall.
It worked I just had to change the built-function direction and it worked but is there a way I can make that when i jump on his head he dies
 
F

FuRyvok

Guest
Sorry, my fault, just change the speed to spd, because the speed is built in varriable
 
F

FuRyvok

Guest
Add this command to the enemy's step event

This will check if the player is about to fall in to the enemy
Code:
//oPlayer is the Player object
if(place_meeting(x,y-1,oPlayer)){
instance_destroy();
}
 
Z

Zeathus

Guest
Add this command to the enemy's step event
Code:
//oPlayer is the Player object
if(place_meeting(x,y-1,oPlayer)){
instance_destroy();
}
Thank you very much and one more question if I add a collision event and put it to the player and make it so that when he touches the player the room restarts will the death still work or do i have to make it differently?
 
F

FuRyvok

Guest
I don't really understand what you want to do, so the when the enemy touches the player, the room has to restart?
 
Z

Zeathus

Guest
I meant I want the player to die when he touches the enemy but if the player touches the top of the enemy that the enemy dies. I'm sorry if i'm boring.
 
F

FuRyvok

Guest
Just add a collision event to the Player object with the enemy
and there just type
Code:
room_restart();
 
F

FuRyvok

Guest
How did you made the Player movement?
Post here the movement code
 
Z

Zeathus

Guest
Code:
//this is the create event
grav = 0.4;
hsp = 0;
vsp = 0;
jumpSpeed = 10;
moveSpeed = 6;
image_index = 0;
image_speed = 0;
Code:
//this is the step event
// Get input
kLeft = -keyboard_check(ord("A"));
kRight = keyboard_check(ord("D"));
kJump = keyboard_check_pressed(ord("W"));

// Use input
move = kLeft + kRight;
hsp = move * moveSpeed;
if (vsp < 10) { vsp += grav; };

if (place_meeting(x, y + 1, Floor_obj)) { vsp = kJump * -jumpSpeed }

// H Collisions
if (place_meeting(x + hsp, y, Floor_obj))
{ while (!place_meeting(x + sign(hsp), y, Floor_obj))
{ x += sign(hsp); }
hsp = 0;
}
x += hsp;

// v Collisions
if (place_meeting(x, y + vsp, Floor_obj))
{ while (!place_meeting(x, y + sign(vsp), Floor_obj))
{ y += sign(vsp); } vsp = 0; }
y += vsp;
 
F

FuRyvok

Guest
Than you have to replace the code in the enemy object
Code:
if(place_meeting(x,y-1,oPlayer)){
instance_destroy();
}
To
Code:
if(place_meeting(x,y-oPlayer.vsp,oPlayer)){
instance_destroy();
}
 
F

FuRyvok

Guest
Than delete the collision event on the player Object

and type this in the player object's step event

Code:
//Collision with enemy
if(place_meeting(x+hsp,y,oEnemy)){
room_restart();
}
 
Z

Zeathus

Guest
Yep,everything worked thank you soo much for the help and like an hour of discussion!
 
I am Working on a platformer game and I tried this code, but my enemy still goes through blocks can someone help me with this?
 
Top