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

Platformer jumping | friction not working (using physics)

L

Little Coding Wolf

Guest
For some reason my character is not jumping. I have looked around and I believe I am using the right code. I put a debug message to check if I hit the code and I do. Second problem is if I set friction variable it seems to not be doing anything. If I set it under the physics window on object it works. I have no idea what is happening.

Character Base Script
Code:
//Input Variables
xInput = 0;
doJump = false;

//Movement Variables
runSpeed = 6;
jumpForce = -60;

//State Variables
isGrounded = false;
jumpCount = 0;
maxJumpCount = 2;

//Image Variables
imageScale = 1;

//Animation Variables
idleSprite = 0;
runSprite = 0;
inAirSprite = 0;

//Physics variables
groundCheckDistance = 10;
moveFriction = 0;
idleFriction = 999;

//Set in-built physics settings for character
phy_fixed_rotation = true;
phy_active = true;
Character Player Input Script
Code:
//Get our input
keyLeft = keyboard_check(vk_left);
keyRight = keyboard_check(vk_right);
keyJump = keyboard_check_pressed(vk_up);

//Set our horzontal movement speed based on input plus walk speed
xInput = (keyRight - keyLeft) * runSpeed;

//Jump if we are press jump key
if (keyJump)
    doJump = true;
Character Movement Script
Code:
//Get if we are grounded
isGrounded = collision_line(x,y,x,y + groundCheckDistance,collision_object, false,false);

//Jump
{
    if (doJump && jumpCount > 1)
    {
        //Penalty for doing first jump in the air
        if (jumpCount == maxJumpCount && !isGrounded)
            jumpCount--;
       
        //Substract jump count by one since we jumped
        jumpCount--;
   
        physics_apply_local_force(x, y, 0, -60);
       
       
        show_debug_message("HIT");
   
    }
    if (isGrounded)
        jumpCount = maxJumpCount;
    doJump = false;
}

//Move X
phy_position_x += xInput;

//Set friction based on if we are moving
friction = phy_speed  != 0 ? moveFriction : idleFriction;
 

TheouAegis

Member
You used _local_force(x,y but _local_force is relative to the origin. So change to 0,0 instead of x,y.

Odd that you are using physics forces for jumping but static forces for running.
 
L

Little Coding Wolf

Guest
So I got it working just fine. The reason why i'm using physics for platformer is because I want to have slopes without writing complex collision system because that's beyond beginner level for the class I am going to teach. Also in Unity I just use the built in physics as well since it works so well. I see game maker has some issues but I think its good enough. Inbuilt friction variable does not seem to work so I used physics_set_friction(0,0). I put 0 for the id and it seems to work just fine. Assuming its the first fixture on object. The origins for the sprites are center bottom for characters. ONLY issue is character slides down slopes but I think its a fair trade for not having to write custom collision.

Character Base script called in create
Code:
//Input Variables
xInput = 0;
doJump = false;

//Movement Variables
runSpeed = 2.75;
jumpForce = 12.5;
fallRate = 0.5;

//State Variables
isGrounded = false;
jumpCount = 0;
maxJumpCount = 2;

//Image Variables
imageScale = 1;

//Animation Variables
idleSprite = 0;
runSprite = 0;
inAirSprite = 0;

//Collision check variables
groundCheckDistance = 10;

//Debuging
doDebug = false;
Character Movement in step
Code:
//Get if we are grounded
isGrounded = collision_line(x,y,x,y + groundCheckDistance,collision_object, false,false);

//Fix our rotaiton so we don't fall over
phy_fixed_rotation = true;

//Jump
{
    if (doJump && jumpCount > 1)
    {
        //Penalty for doing first jump in the air
        if (jumpCount == maxJumpCount && !isGrounded)
            jumpCount--;
       
        //Substract jump count by one since we jumped
        jumpCount--;
       
        //Jump by setting speed y to -(We want to go up)
        phy_speed_y = -jumpForce;
    }
   
    //We are grounded so reset our jump count
    if (isGrounded)
        jumpCount = maxJumpCount;
}

//Move X
phy_speed_x = xInput * runSpeed;
   
//Give a force downards to keep us on the ground
if (isGrounded && phy_speed_y >= 0)
    phy_speed_y = 1;
   
//Fall
else phy_speed_y += fallRate;

//Set Friction to 0 so we don't get stuck on walls in air
physics_set_friction(0,0)
I use a new grounded variable for animation that is a little bit longer distance so our sprite does not keep changing from in air to grounded really fast over how a character could be on ground visually but not by physics says.

Code:
var aboutToBeGrounded = collision_line(x,y,x,y + groundCheckDistance + 5,collision_object, false,false);

if(aboutToBeGrounded)
{
    if (xInput == 0) sprite_index = idleSprite; else sprite_index = runSprite;
    image_speed = 1;
}

//InAir Animation
else if (!aboutToBeGrounded)
{
    if (sign(phy_speed_y)) > 0 image_index = 1; else image_index = 0;
    sprite_index = inAirSprite;
    image_speed = 0;
}

//Face Character
if (xInput != 0)
    image_xscale = sign(xInput) * imageScale;
Debug Code in DrawEnd
Code:
//Toggle debugging (Not in player input since we want to be able to toggle on all objects)
if (keyboard_check_pressed(vk_tab))
 doDebug = ! doDebug;

//Exit if we are not to do debugging
if (!doDebug)
 exit;

//Debug physics shapes
physics_draw_debug();

//Debug draw line for ground check
draw_line_color(x,y,x,y + groundCheckDistance,c_green,c_green);
Code:
//Get our input
keyLeft = keyboard_check(vk_left);
keyRight = keyboard_check(vk_right);
keyJump = keyboard_check_pressed(vk_up);

//Set our horzontal movement speed based on input plus walk speed
xInput = (keyRight - keyLeft) * runSpeed;

//Jump if we are press jump key
if (keyJump)
    doJump = true;
else doJump = false;
 
L

Little Coding Wolf

Guest
You used _local_force(x,y but _local_force is relative to the origin. So change to 0,0 instead of x,y.

Odd that you are using physics forces for jumping but static forces for running.
Thanks for the reply. I feel stupid. Anyway I used phy_speed_y since its easier to understand for others who are a beginner.
 
Top