Godot Version
v4.2.1.stable.official [b09f793f5]
Question
Why does my code doesn’t work, and the character doesn’t move at all?
Even while in the air, it doesn’t fall.
extends CharacterBody2D
# Physics variables on the ground.
# The X ones.
const maxWalkingSpeed = 1308
const walkingAcceleration = 136
const airControl = 32
const friction = 51
#The Y Ones
const maxFallingSpeed = 1536 # Which is, also, max gravity for all the critters in the game. Take care!
const gravity = 80
const jumpingGravity = 32
const jumpSpeed = 1280
func _physics_process(delta):
handle_maxWalkingSpeed(delta)
handle_walkingAcceleration(delta)
handle_airControl(delta)
handle_friction(delta)
handle_maxFallingSpeed(delta)
handle_gravity(delta)
handle_jumpingGravity(delta)
handle_jumpSpeed(delta)
# Next up - all that physics jazz.
# X Physics Functions
func handle_maxWalkingSpeed(delta):
if velocity.x > maxWalkingSpeed:
velocity.x = maxWalkingSpeed
func handle_walkingAcceleration(delta):
if is_on_floor():
if Input.is_action_pressed("Right"):
velocity.x += walkingAcceleration
if Input.is_action_pressed("Left"):
velocity.x -= walkingAcceleration
func handle_airControl(delta):
if !is_on_floor():
if Input.is_action_pressed("Right"):
velocity.x += airControl
if Input.is_action_pressed("Left"):
velocity.x -= airControl
func handle_friction(delta):
if is_on_floor:
if velocity.x != 0:
velocity.x - friction
# Y Physics Functions
func handle_maxFallingSpeed(delta):
if velocity.y < maxFallingSpeed:
velocity.y = maxFallingSpeed
func handle_gravity(delta):
if !Input.is_action_pressed("Jump") || (velocity.y < 0):
velocity.y -= gravity
func handle_jumpingGravity(delta):
if (velocity.y > 0) && Input.is_action_pressed("Jump"):
velocity.y -= jumpingGravity
func handle_jumpSpeed(delta):
if Input.is_action_pressed("Jump") && is_on_floor():
velocity.y = jumpSpeed