• 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 Momentum and Crouching Problems

L

lolols...

Guest
For a while I have been having trouble to create a fully functional crouching animation for my player character. Just recently I have found a bit of a solution but it is a bit buggy, I'll get to that later.

Right now I have a very important question. How do I properly implement friction in my game? I've tried a few ways but none of them work at all. The reason why I'm asking this along with crouching is because I want it so that the amount of friction increases when my character crouches. The way I want to have it is like being on ice except not as slippery. How would I do this? Huge thanks in advance.

Also, if anybody is interested in solving any bugs with my crouching you could fix, it would be a huge help. You don't need to do it if you don't want to, but if you do, the main bug I'm concerned about is the player quickly switching to its idle animation for a split second while in mid-air if you try to crouch.

Here is my player's code:

Create Event:
execute code:

///Initialize Variables
grav = 0.4;
hsp = 0;
hsp_carry = 0;
vsp = 0;
movespeed = 1.7;
jumps = 0;
jumpsmax = 2;
jumpspeed_normal = 6;
jumpspeed = jumpspeed_normal
key_down = 0;
ladder = false;

Step Event:
execute code:

///Basic Movement
//Get the player's input
key_right = keyboard_check(vk_right);
key_left = -keyboard_check(vk_left);
key_jump = keyboard_check_pressed(ord('X'));
key_jump_held = keyboard_check(ord('X'));
key_attack = keyboard_check(ord('Z'));
key_down = keyboard_check(vk_down);
key_up = keyboard_check(vk_up);

//React to inputs
move = key_left + key_right;
hsp = move * movespeed;
if (vsp < 10) vsp += grav

//jumping
if (place_meeting(x,y+1,obj_wall))
{
jumps = jumpsmax;
}
else
{
if (jumps == jumpsmax) jumps -=1;
}

if (key_jump) && (jumps > 0)
{
jumps -= 1;
vsp = -jumpspeed;
}

if (vsp < 0) && (!key_jump_held) vsp = max(vsp,-jumpspeed/2)


var hsp_final = hsp + hsp_carry;
hsp_carry = 0;

//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;

//Ladder
if (key_up)
{
if place_meeting(x,y,obj_ladder) ladder = true;
}
if (ladder)
{
vsp = 0;
//hsp = 0;
if (key_up) vsp = -2;
if (key_down) vsp = 2;
if !place_meeting(x,y,obj_ladder) ladder = false;
if (key_jump) ladder = false;
}

//Animate
if (move!=0) image_xscale = move;
if (place_meeting(x,y+1,obj_wall))
{
if (hsp!=0) sprite_index = spr_player_walk; else sprite_index = spr_player_idle;
}
else
{
if (vsp < 0) sprite_index = spr_player_jump ; else sprite_index = spr_player_fall;
}
{
if ladder = true sprite_index = spr_player_ladder;
}
{
if (move!=0) image_speed = 0.3; else image_speed = 0.4;
}
//Crouching
if keyboard_check(vk_down) and (place_meeting(x,y+1,obj_wall))
{
sprite_index = spr_player_crouch;
mask_index = spr_player_crouch;
movespeed = 0;
jumpsmax = 0;
}
else
if keyboard_check_released(vk_down)
{
sprite_index = spr_player_idle;
mask_index = spr_player_idle;
movespeed = 1.7;
jumpsmax = 2;
}

execute code:

///Camera Movement
view_xview[0] += (((x-(view_wview[0]/2)) - view_xview[0]) * 0.5);
view_yview[0] += (((y-(view_hview[0]/2)) - view_yview[0]) * 0.5);

//Parallax Scrolling

background_x[0] = view_xview[0] * 0.05
background_x[1] = view_xview[0] * 0.1
background_x[2] = view_xview[0] * 0.2
background_x[3] = view_xview[0] * 0.3
background_x[4] = view_xview[0] * 0.4
background_x[5] = view_xview[0] * 1.2
 
S

Supercoder

Guest
I admit I'm replying without reading through all of your code (>.<), but you could do something like hsp/=1.1 to increase the friction. Just use an if statement to check if the character is crouching and divide by more.
 
L

lolols...

Guest
I admit I'm replying without reading through all of your code (>.<), but you could do something like hsp/=1.1 to increase the friction. Just use an if statement to check if the character is crouching and divide by more.
Ok... Where exactly do I use your hsp thing because the player's movement speed is controlled by the variable "movespeed" so I input it in the create code and my game just crashes. Then I put it into the actual hsp variable which is also in the create code and again, the game won't work. Am I doing something wrong here or its something else?
 
S

Supercoder

Guest
That code should be run in the step event, preferably after all of the other code there. Try something like this:
Code:
if CROUCHVARIABLE==true
{
       SPEED/=2
}
else
{
       SPEED/=1.1
}
You might want to consider putting in an if to check if the player is on the ground as well, so it isn't slowing down while jumping.
 
L

lolols...

Guest
That code should be run in the step event, preferably after all of the other code there. Try something like this:
Code:
if CROUCHVARIABLE==true
{
       SPEED/=2
}
else
{
       SPEED/=1.1
}
You might want to consider putting in an if to check if the player is on the ground as well, so it isn't slowing down while jumping.
Okay, well I put in your code and it works... sorta. All that happens is that the player's momentum works instead of the usual moving and then the momentum coming into play. All I wanted there to be is when the player stops moving, there's momentum that happens and the momentum is larger if the player moves and then crouches.

Also I took your advice on the player turning into the idle sprite in mid-air and it worked, all I needed to do was put the on-ground variable on the "else" part of the players' crouch code as well so thank you for helping me with that.
 
Last edited by a moderator:
S

Supercoder

Guest
Hmm... I'm not sure how to manage that. You would have to use some if statements to check if it's crouching for sure, but beyond that I'm not sure.
 
L

lolols...

Guest
Hmm... I'm not sure how to manage that. You would have to use some if statements to check if it's crouching for sure, but beyond that I'm not sure.
Well sorry about that. Maybe I had a too complex explanation? Well if so, the best way I would explain it in code would be something like a variable that would go along with the momentum/friction code and it would be something like momentum = 1 and if the player were to crouch it would be momentum = 2 or something like that.
 
Top