My physics don't work as intended

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

You’re missing the () from is_on_floor.
Also wouldn’t subtracting the friction make you move faster if you’re moving left?
You could do velocity.x = move_toward(velocity.x, 0.0, friction) to make it move toward 0 whether it’s positive or negative.

edit: Also, using acceleration is going to be floaty. You can increase the acceleration and friction to make it more “responsive”, less floaty.

1 Like

Thank you so much for such a helpful answer!
Also, could you take a look in my Y physics code? It seems that one of the issues is that player can’t jump or accelerate towards Y axis for some reason.

I think maxFallingSpeed is supposed to be a negative value?

Right now I think this part of the code always sets velocity.y to maxFallingSpeed because velocity.y is always lesser than 1308:

func handle_maxFallingSpeed(delta):
	if velocity.y < maxFallingSpeed:
		velocity.y = maxFallingSpeed

I might be wrong, I don’t use 2D, so it’s confusing for me that the Y axis is upside down in 2D :smile:
edit: actually it might be the other way around, the jumpSpeed needs to be negative, since “up” is negative in 2D, not sure…

1 Like

You’re right - negative value really fixed jumpSpeed, yet had no effect on maxFallingSpeed. Currently thinking how should I assign the values because they are so HARD to maintain.

Thank you for your help, and you’re helping pretty good on 2D part despite not using it much!

1 Like

Albeit fixed, jump still doesn’t work like needed, and the player only does small one-frame jumps, which is not a good thing at all.