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

Legacy GM need help with platformer ice block

E

EZTALES

Guest
hey yall,
needing some help with an ice block, here is the code. i want my player to slide on the block.
CREATE OF PLAYER:
///intialize varibles
grav = 0.7;
hspd = 0;
vspd = 0;
jumpspeed_normal = 8;
jumpspeed_powerup = 10;

jspd = jumpspeed_normal;
speed_normal = 5;
speed_powerup = 7;
spd = speed_normal;
// note that there is no physics in room, therefore do not turn on physics.
STEP:

if (vspd < 0) && (!key_jump_held) vspd = max(vspd,0)


//moving right
if (rkey) {
hspd = spd
}

//moving left
if (lkey) {
hspd = -spd
}

// check for not moving
if ((!rkey && !lkey) || (rkey && lkey)) {
hspd = 0;
}

//Horizontal collisions
if (place_meeting(x+hspd, y, obj_ice)) {
while (!place_meeting(x+sign(hspd), y, obj_ice)) {
x+= sign(hspd);
}
hspd = 0;
}

//move horizontally
x += hspd;

//vertical collisions
if (place_meeting(x, y+vspd, obj_ice)) {
while (!place_meeting(x, y+sign(vspd), obj_ice)) {
y+= sign(vspd);
}
vspd = 0;
}

//move vertically
y += vspd;
/// any help would be great! examples, etc.
 

flerpyderp

Member
Look into acceleration and friction. There are plenty of examples around. Here is just one example of how to handle friction for when the keys are released.
Code:
var fric = 0.95; //Change to suit

// check for not moving
if ((!rkey && !lkey) || (rkey && lkey)) {
if (place_meeting(x,y+1,obj_ice){
hspd *= fric;
}
else hspd = 0;
}
 
Last edited:

flerpyderp

Member
Try changing the fric variable to a lower value. The code I posted will check if there is an ice block below the player, and if so, gradually decrease the hspd to a halt. This only affects the player's speed when the check for both keys off or both keys on is true. It's just a basic example, again I advise looking at the many examples of acceleration and friction for simple platformer movement that already exist.
 

TheouAegis

Member
//Horizontal collisions
if (place_meeting(x+hspd, y, obj_ice)) {
while (!place_meeting(x+sign(hspd), y, obj_ice)) {
x+= sign(hspd);
}
hspd = 0;
}

First of all, with that code I quoted above in your project, can you even move normally without the sliding? The code I quoted should only make him stop when ice is directly in front of him, like a wall; it should not affect the player at all if there is only ice below him. If Ice below the player is actually making the player not move horizontally at all, then the issue is that the player has fallen into the ice. That's one issue you would need to take care of before anything else. If the player can move while standing on Ice just as you would while standing on any other type of ground, then and only then can you progress to friction and surface resistance.

Furthermore, your ice object should probably be parentage to your normal ground object, because ice is just normal ground with a friction attribute. Think about it for a sec, whether or not the block is made of ice is insignificant when dealing with collisions. You only care about the ice when accelerating or decelerating gradually, not abruptly.

Your original code has no friction or acceleration (except gravity).
if rkey hspd = spd;
That does nothing to take the surface upon which the player is walking into consideration. It simply says if the player tries to move, immediately set his movement speed to the highest possible value. flerpyderp's code handles the stopping by reducing hspd by 5% each step that ice is underfoot.
 
Top