Godot Version
v4.2.1.stable.official [b09f793f5]
Question
My physics don’t work as intended, it’s all floaty and the friction is endless for some reason.
Can someone review my code and tell me what’s wrong and what should be changed?
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)
move_and_slide()
# 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