To add gravity to your script, you will need to apply a downward force to the velocity over time. This typically means increasing the y component of the velocity vector on each frame Update. In Godot, move_and_slide handles gravity if you pass a floor_normal vector and use the velocity property for vertical movement.
Here's an example of how you might add gravity to the script (assuming the gravity is a constant value and that we're working with a KinematicBody2D node which has move_and_slide method):
extends KinematicBody2D
var velocity = Vector2() # Initial velocity
var gravity = 10 # Gravity value
var current_speed = 100 # Speed of the character
var jump_power = -300 # Power of a jump, should be negative to go upwards
var floor_normal = Vector2(0, -1) # Assuming the floor is always below
# Function that will execute movement commands with gravity
func execute(s, dir = []):
# Applying gravity to velocity
velocity.y += gravity
# Handling horizontal movement
if dir.has("left"):
velocity.x = -current_speed
elif dir.has("right"):
velocity.x = current_speed
else:
velocity.x = 0 # No horizontal movement
# Handling jump
if dir.has("jump") and is_on_floor():
velocity.y = jump_power
# Move the character and apply sliding motion (for sloped surfaces and collisions)
velocity = move_and_slide(velocity, floor_normal)
Remember that in Godot's coordinate system, the Y-axis points downward, so positive gravity will make the character fall downward, and negative values in velocity.y will make the character move upward.
Please adjust the gravity, current_speed, and jump_power to match the intended physics of your game. This example assumes that execute will be called every frame in the _physics_process function.
The above script can be improved by adding delta time (delta) if you want the custom gravity and motion to be frame rate independent:
extends KinematicBody2D
var velocity = Vector2() # Initial velocity
var gravity = 980 # Gravity value (pixels per second squared)
var current_speed = 100 # Speed of the character in pixels per second
var jump_power = -300 # Power of a jump, should be negative to go upwards
var floor_normal = Vector2(0, -1) # Assuming the floor is always below
# Function that will execute movement commands with gravity
func execute(delta, dir = []):
# Applying gravity to velocity (scaled by delta)
velocity.y += gravity * delta
# Handling horizontal movement
if dir.has("left"):
velocity.x = -current_speed
elif dir.has("right"):
velocity.x = current_speed
else:
# This allows for smoother stops, using linear interpolation
velocity.x = lerp(velocity.x, 0, 0.5)
# Handling jump
if dir.has("jump") and is_on_floor():
velocity.y = jump_power
# Move the character and apply sliding motion (for sloped surfaces and collisions)
velocity = move_and_slide(velocity, floor_normal)
# Make sure to call the execute function in _physics_process
func _physics_process(delta):
# Retrieve the direction from input or other sources
var direction = [] # Get directional input into this variable
execute(delta, direction)
Be sure to adjust your _physics_process to pass delta to the execute function, and handle the input to put values into the direction variable.