Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-08-06· OpenAIAi2 WildChat-4.8M (AI2)

in gamemaker 2 gml, include a tutorial, give me a amazingly advanced gravity script that…

  1. User

    in gamemaker 2 gml, include a tutorial, give me a amazingly advanced gravity script that detects when it touches an object or falls, and also make it not janky, make it fluid and smooth, and accurate to irl, make sure that you tell me where to put stuff and also include a jumping function

  2. Assistant

    Creating a highly advanced gravity and jumping system in GameMaker Studio 2 using GML can be a fun and rewarding experience. For a smooth and fluid movement system, we need to ensure that the gravity, collision detection, and jumping mechanics are seamlessly integrated. Below is a step-by-step guide to help you implement this system:

    Setup

    1. Create an Object for the Player

      • Create a new object in GameMaker, let's call it obj_player.
      • Assign a sprite to your player if you have one.
    2. Define Constants and Variables

      • Open the Create Event for obj_player and add the following GML code:
      // Constants
      grav_acceleration = 0.5; // gravity acceleration
      max_fall_speed = 12;     // terminal velocity (max falling speed)
      jump_speed = -10;        // initial jump speed
      walk_speed = 4;          // player walking speed
      is_on_ground = false;    // to check if player is on ground
      
      // Movement variables
      hsp = 0; // horizontal speed
      vsp = 0; // vertical speed
      
    3. Implement Gravity and Movement

      • Open the Step Event for obj_player and add the following code:
      // Horizontal Movement
      if (keyboard_check(vk_left)) {
          hsp = -walk_speed;
      } else if (keyboard_check(vk_right)) {
          hsp = walk_speed;
      } else {
          hsp = 0;
      }
      
      // Apply gravity
      if (!is_on_ground) {
          vsp += grav_acceleration;
      }
      
      // Limit vertical speed to terminal velocity
      if (vsp > max_fall_speed) {
          vsp = max_fall_speed;
      }
      
      // Horizontal collision detection
      if (place_meeting(x + hsp, y, obj_solid)) {
          while (!place_meeting(x + sign(hsp), y, obj_solid)) {
              x += sign(hsp);
          }
          hsp = 0;
      }
      x += hsp;
      
      // Vertical collision detection
      if (place_meeting(x, y + vsp, obj_solid)) {
          while (!place_meeting(x, y + sign(vsp), obj_solid)) {
              y += sign(vsp);
          }
          vsp = 0;
          is_on_ground = true;
      } else {
          is_on_ground = false;
      }
      y += vsp;
      
    4. Add Jump Functionality

      • Add this code to the same Step Event to handle jumping:
      if (keyboard_check_pressed(vk_up) && is_on_ground) {
          vsp = jump_speed;
          is_on_ground = false;
      }
      
    5. Create an Object for Solid Platforms

      • Create another object called obj_solid.
      • Assign a solid sprite if desired and set it as solid in the object properties.

    Explanation

    • Gravity: We increment the vsp (vertical speed) by grav_acceleration when the player is not on the ground, making the player fall.
    • Collision Detection: We use place_meeting to check for horizontal and vertical collisions with obj_solid. This ensures smooth movement by moving the player up to the point of collision without overlapping.
    • Terminal Velocity: The player's fall speed is capped at max_fall_speed to simulate a realistic fall.
    • Jumping: When the up key is pressed and the player is on the ground, vsp is set to a negative value to simulate a jump.

    Additional Tips

    • Tweak the gravity, jumping, and walking speeds to match your game's feel.
    • For more advanced handling, consider adding friction and acceleration for smoother starts and stops.
    • Play with animation states to visualize when the player is jumping, falling, or running.

    This system should provide a smooth and realistic jumping and gravity experience. Make sure to test and adjust the values for your specific game needs.