Different ways to handle movement

Version 4.4.1

Currently making a new 3D project, while looking for ways to handle the movement I found that most people used:

var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
	velocity.x = direction.x * SPEED
	velocity.z = direction.z * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)
	velocity.z = move_toward(velocity.z, 0, SPEED)
move_and_slide()

But while experimenting I found I could use

var input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_back")
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
velocity = direction * speed

move_and_slide()

And it also managed to work as a movement system. Are there any major differences between these two methods of handling player movement?

Don’t use the ui_… ones but the move_… that you have defined. It’s simply much cleaner, more descriptive and professional looking. At a later point you might allow to player to change their key-bindings, then changing just the move_… action is what you want to allow the player to do.

Ah yes that code isn’t mine, I took the snippet from another forum post here as an example of what I was talking about. I am planning to use the custom mapping for my inputs.

The first method adds deceleration when there’s no input by using move_toward(velocity.x, 0, SPEED), which makes movement feel smoother and more natural.

The second method directly sets velocity based on input. It works, but there’s no deceleration, which means movement stops instantly when input stops.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.