Godot Version
v4.5.stable.mono.official [876b29033]
Question
I know this is basic question, but I’m finding straight up contradictory information about it, even on this forum.
For presentation I will use code from this tutorial (which is very nice btw, I highly recommend checking it out if you are beginner) GitHub - Una1n/Platformer2DMovementTutorial at End-Part2
What I have managed to find out:
- Use _physics_process if you do anything with physics, use _process in every other scenario. Makes sense. But both of them provide delta.
- You should use delta at all times, except when modifying velocity, because then it happens on it’s own. Okay, understandable.
Example code:
func handle_horizontal_movement(body: CharacterBody2D, direction: float) -> void:
var velocity_change_speed: float = 0.0
if body.is_on_floor():
velocity_change_speed = ground_accel_speed if direction != 0 else ground_decel_speed
else:
velocity_change_speed = air_accel_speed if direction != 0 else air_decel_speed
body.velocity.x = move_toward(body.velocity.x, direction * speed, velocity_change_speed)
Delta is not used, because we only modify velocity. Makes sense. Except I’ve found post with identical formula, and they said that actually in this case you should multiply velocity_change_speed by delta. This contradicts the previous rules.
Another example code:
func handle_gravity(body: CharacterBody2D, delta: float) -> void:
if not body.is_on_floor():
body.velocity.y += gravity * delta
is_falling = body.velocity.y > 0 and not body.is_on_floor()
We again only modify velocity, but this time we do multiply by delta. The same codebase, the same operation, completely different rules.
Even when you look at automatically generated template, things are mixed up:
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
# 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()
Of course, I do not imply that everyone is wrong except me, I just want to know what are the true rules of how things work. Because it looks like a lot of people don’t understand it either, and post incorrect information as truth.