Godot Version
4
Question
So I’m making a 2D platformer with a focus on being able to jump when on slopes and in the air but not when on a flat surface. I’m trying to implement the mechanic that lets the player get increased distance when jumping off of a slope. Here’s my code (I’m still a beginner so my code might be kinda trash)
extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -550
Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)
var jump_count = 0
var jump_max = 1
var slide_on_slope = true
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
# Handle jump.
if is_on_floor() and jump_count!=0:
jump_count = 0
#checks jump count and such
if jump_count<jump_max:
if Input.is_action_just_pressed("ui_accept") and not is_on_floor():
velocity.y = JUMP_VELOCITY
jump_count+=1
if slide_on_slope == true:
if Input.is_action_just_pressed("ui_accept"):
velocity.y = JUMP_VELOCITY
velocity.x = SPEED * 2
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("ui_left", "ui_right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()