Shawn Basnett
Discount Dev
GM Version: Game Maker Studio 1 (should work in v.2)
Target Platform: PC (should work for HTML5 and mobile modules)
Description: character movement with momentum, wall sliding, and wall jumping
After a bit of work, I've created a version of Shaun Spalding's player code that includes momentum, wall sliding, and wall jumping.
First, in the Create event, Add two new variables; max_hsp, and normal_grav
lower your movespeed and your grav variable, your Create Event should be similar to this:
max_hsp will provide as a cap to your speed left or right, this will keep you from shooting through the wall by keeping your momentum under the number you set it to.
normal_grav is a sort of reference for the wall sliding later implemented.
Next, Change your Step event to this:
This code should cause your player to accelerate until it hits maximum speed in any direction.
Wall jumping is set up to carry up the momentum of the player, this means that if the player is to move at maximum speed to the right and jump at and off of the right wall, the momentum will carry the player higher than if it were to just jump at it from a still position.
The Wall sliding Mechanic is simply there to compliment the wall jump. When the player holds the movement key towards the right, as long as the player's vsp is positive, the player will slowly lose momentum until they hit the floor, this loss of momentum caps off at a vsp of 1.5 and a grav of .05
[Issues to Note]
For some reason, if the player hits terminal velocity going down (I believe it to be 16), The momentum on the wall slide will not decrease.
If wall jumps are done consecutively on walls that are too close together, the vsp will continue to decrease until you either hit a ceiling or shoot through it.
The Collision code should be added in as well, his collision code should work perfectly with this.
I know it's not perfect and it needs cleaned up a bit, but it's a basis for people to get an understanding of how it could be done.
Target Platform: PC (should work for HTML5 and mobile modules)
Description: character movement with momentum, wall sliding, and wall jumping
After a bit of work, I've created a version of Shaun Spalding's player code that includes momentum, wall sliding, and wall jumping.
First, in the Create event, Add two new variables; max_hsp, and normal_grav
lower your movespeed and your grav variable, your Create Event should be similar to this:
Code:
///Initialize variables
grav = 1;
hsp = 0;
vsp = 0;
jumpspeed = 11;
movespeed = .5;
max_hsp = 7;
normal_grav = 1;
normal_grav is a sort of reference for the wall sliding later implemented.
Next, Change your Step event to this:
Code:
///Get the Player's Input
key_right = keyboard_check(vk_right);
key_left = -keyboard_check(vk_left);
key_jump = keyboard_check_pressed(vk_up);
//React to the player's inputs
move = key_left + key_right;
if (key_left = -1) previous_dir = -1;
if (key_right = 1) previous_dir = 1;
//Acceleration
if (hsp < max_hsp) && (hsp > -max_hsp)
{
hsp += move * movespeed;
}
else if (hsp = max_hsp)
{
if (key_right)
{
hsp = max_hsp;
}
else
{
hsp -= 1
}
}
else if (hsp = -max_hsp)
{
if (key_left)
{
hsp = -max_hsp;
}
else
{
hsp += 1;
}
}
if (hsp > 0) && (key_left = 0) && (key_right = 0) && (place_meeting(x,y+1,obj_wall)) {hsp -= .5}
if (hsp < 0) && (key_left = 0) && (key_right = 0) && (place_meeting(x,y+1,obj_wall)) {hsp += .5}
//Gravity
if (vsp < 10) vsp += grav;
if (place_meeting(x,y+1,obj_wall))
{
vsp = key_jump * -jumpspeed
}
//Wall Jumps
if (place_meeting(x+1,y,obj_wall)) && (!place_meeting(x-1,y,obj_wall))
{
if (key_jump) && (!place_meeting(x,y+1,obj_wall))
{
vsp -= 15;
hsp -= 5;
}
}
if (place_meeting(x-1,y,obj_wall)) && (!place_meeting(x+1,y,obj_wall))
{
if (key_jump) && (!place_meeting(x,y+1,obj_wall))
{
grav = normal_grav;;
vsp -= 15;
hsp += 5;
}
}
//Wall Slides Left
if (key_left = -1) && (vsp > 0) && (place_meeting(x-1,y,obj_wall)) && (!place_meeting(x,y+1,obj_wall))
{
if (vsp <= 11) && (vsp > 1.5) vsp -= 1;
if (vsp <= 11) && (vsp > 0) grav = .05;
}
if (key_left = -1 && (place_meeting(x-1,y,obj_wall)) && (!place_meeting(x,y+1,obj_wall)))
{
grav = normal_grav;
}
if (key_left = 0)
{
grav = normal_grav;
}
//Wall Slides Right
if (key_right = 1) && (vsp > 0) && (place_meeting(x+1,y,obj_wall)) && (!place_meeting(x,y+1,obj_wall))
{
if (vsp <= 16) && (vsp > 1.5) vsp -= 1;
if (vsp < 10) && (vsp > 0) grav = .05;
}
if (key_right = 1 && (place_meeting(x+1,y,obj_wall)) && (!place_meeting(x,y+1,obj_wall)))
{
grav = normal_grav;
}
if (key_right = 0)
{
grav = normal_grav;
}
//Horizontal Collision
if (place_meeting(x+hsp,y,obj_wall))
{
while(!place_meeting(x+sign(hsp),y,obj_wall))
{
x += sign(hsp);
}
hsp = 0;
}
x += hsp;
//Vertical Collision
if (place_meeting(x,y+vsp,obj_wall))
{
while(!place_meeting(x,y+sign(vsp),obj_wall))
{
y += sign(vsp);
}
vsp = 0;
}
y += vsp;
Wall jumping is set up to carry up the momentum of the player, this means that if the player is to move at maximum speed to the right and jump at and off of the right wall, the momentum will carry the player higher than if it were to just jump at it from a still position.
The Wall sliding Mechanic is simply there to compliment the wall jump. When the player holds the movement key towards the right, as long as the player's vsp is positive, the player will slowly lose momentum until they hit the floor, this loss of momentum caps off at a vsp of 1.5 and a grav of .05
[Issues to Note]
For some reason, if the player hits terminal velocity going down (I believe it to be 16), The momentum on the wall slide will not decrease.
If wall jumps are done consecutively on walls that are too close together, the vsp will continue to decrease until you either hit a ceiling or shoot through it.
The Collision code should be added in as well, his collision code should work perfectly with this.
I know it's not perfect and it needs cleaned up a bit, but it's a basis for people to get an understanding of how it could be done.
Last edited: