Collision issues!!!

V4NTT4BL4CK

Member
I've been working on this prototype minigame to learn how to use Game Maker Studio 2, but I have a problem that I don't have with other '' walls '' or colliding objects ...



when I jump to the roof or platform, my character should stop and return to the ground when he touches the object but returns when he has already crossed the middle of the platform
help 1.png
imagen_2021-07-27_014124.png

This is my code for gravity:

if (!collision_rectangle(x-8,y,x+8,y+1,obj_block, false,false)) {
gravity = 0.10;
image_index = spr_Jump;
}

if (vspeed > 0) {
var ground = collision_rectangle(x-8,y,x+8,y+vspeed,obj_block, false,false);
if (ground) {
y = ground.y;
vspeed = 0;
gravity = 0;
}
} else if (vspeed < 0) {
var ceiling = collision_rectangle(x-8,y-0,x+8,y-0+vspeed,obj_block,false,false);
if (ceiling) {
y = ceiling.y + ceiling.sprite_height + 25;
vspeed = 0;

}

}

And this is my code for jump:


if (keyboard_check_pressed(vk_space) && collision_rectangle(x-8,y,x+8,y+25,obj_block, false,false)) {
vspeed = -10;
image_index = spr_Jump;
}
 

Slyddar

Member
vpseed, hspeed and gravity are not your players friend if you are making a platform game.

There are plenty of tutorials on making a platformer, even Yoyo has official ones, so maybe try one of those as they show the standard way of doing it without using these variables, as they don't suit player characters for platformers due to their "always on" status.
 

Nidoking

Member
var ground = collision_rectangle(x-8,y,x+8,y+vspeed,obj_block, false,false);
var ceiling = collision_rectangle(x-8,y-0,x+8,y-0+vspeed,obj_block,false,false);
You're checking the same rectangle twice. -0 means nothing at all, so get rid of it. If you want to check in the opposite direction of vspeed, you have to subtract vspeed. But even then, you're mishandling your ground and ceiling checks, because when vspeed is negative, the "ground" and "ceiling" are reversed. And you're also not checking the correct side of your bounding box. It looks like, instead of taking the sign of vspeed into account and checking up or down as necessary, you've tried to do both. Read up on the sign function in the manual and the bbox_ variables to help you work out how to do the proper check based on the direction you're actually moving.
 
Top