GMS 2 Stopping only if feet are touching the ground.

F

FoolishTom

Guest
Am still very new to Game Maker Studios, and have minimal experience with programming altogther, but thought i would give it a try.

I'm Currently trying to make a game similar to Mario (something i know that can show me a lot of the basics), but have come across a problem with my collisions on the ground.

So up till now i have been using:

if (place_meeting(x,y+vsp,obj_floor))
{
while(!place_meeting(x,y+sign(vsp),obj_floor))
{
y += sign(vsp);​
}
vsp = 0;​
}​

but this does not allow me to jump up through the ground to a level higher. So i added after:

if (vsp > 0)
{
if (place_meeting(x,y+vsp,obj_floor_thru))
{
while(!place_meeting(x,y+sign(vsp),obj_floor_thru))
{
y += sign(vsp);​
}
vsp = 0;​
}​
}​

This gets me closer, but if i stop moving up at any point i come to a halt, and am basically stuck in the wall.

Any suggestions on a change, that will only stop my movement downward only if my feet are touching the top of the floor.

Thanks in Advance,
FoolishTom
Noobie
 
R

Rabid_Ghost

Guest
so your trying to make a wall that you can jump up through but its solid on top?
 
Last edited by a moderator:
F

FoolishTom

Guest
so your trying to make a wall that you can jump up through but its solid on top?
yes, there is gravity on this object so it is always moving in a positive vertical speed (vsp), downward, unless it is touching the ground (obj_floor), in the second line of code, i allow the object to pass through the bottom of the floor, but as soon as the object stops moving upwards from a jump the vsp will turn to 0, stopping any gravity. Even if only the top of the object touches the floor.

In the picture, the greener part is the floor one can pass through from the bottom, but because the player, the blue square, touches the green gravity stops effecting it because it is touching the floor.
 

Attachments

R

Rabid_Ghost

Guest
yes, there is gravity on this object so it is always moving in a positive vertical speed (vsp), downward, unless it is touching the ground (obj_floor), in the second line of code, i allow the object to pass through the bottom of the floor, but as soon as the object stops moving upwards from a jump the vsp will turn to 0, stopping any gravity. Even if only the top of the object touches the floor.

In the picture, the greener part is the floor one can pass through from the bottom, but because the player, the blue square, touches the green gravity stops effecting it because it is touching the floor.
Ok, is your collision stopping happening after your are in the floor and moving you back out, or is it predicting that you will collide and moving you to the edge. also if you could send the code i could just fix the code.
 
F

FoolishTom

Guest
//initialize variables

grav = 1.9;
hsp = 0;
vsp = 0;
grounded = 0;
jumpspeed = 19;
movespeed = 16;
accel = 1;

//Get Input
key_right = keyboard_check(vk_right);
key_left = keyboard_check(vk_left);
key_jump = keyboard_check_pressed(vk_space);

//React to input
if (key_right)
{
if (hsp < movespeed)
{
hsp += accel;
}
}
if (key_left)
{
if (hsp > -movespeed)
{
hsp -= accel;
}
}

//Deceleration to stop
if ((!key_left && !key_right) or (key_left && key_right)) and (grounded = 0)
{
hsp += (0 - hsp) * accel/1.5;
}
if ((!key_left && !key_right) or (key_left && key_right)) and (grounded = 1)
{
hsp += (0 - hsp) * accel/3;
}

//Gravity
if (vsp < 15 && !place_meeting(x,y,obj_floor))
{
vsp += grav;
}

//Jumping
if (place_meeting(x,y+1,obj_floor))
{
vsp = key_jump * -jumpspeed;
grounded = 1;
}

//Horizontal Collision
if (place_meeting(x+hsp,y,obj_floor))
{
while(!place_meeting(x+sign(hsp),y,obj_floor))
{
x += sign(hsp);
}
hsp = 0;
}
x += hsp;

//Vertical Collision
if (place_meeting(x,y+vsp,obj_floor))
{
while(!place_meeting(x,y+sign(vsp),obj_floor))
{
y += sign(vsp);
}
vsp = 0;
grounded = 0;
}

//Vertical Collision Through from Below
if (vsp > 0)
{
if (place_meeting(x,y+vsp,obj_floor_thru))
{
while(!place_meeting(x,y+sign(vsp),obj_floor_thru))
{
y += sign(vsp);
}
vsp = 0;
}
}
y += vsp;

This is my current player events. the last if statement, in the step event, is where i allows the player to go through from below, but not through from above. Ive also found the problem that if i am in contact with that object, i cannot jump either.

If you do make changes, could you explain a little if you have time. Want this to be learning experience as well.

Thanks,
Foolish
 
Top