The input function seems good, other than the mis-capitalized name, you may notice the function is missing it’s blue arrow by the line number; this is the correct signature:
func _input(event: InputEvent) -> void:
Your move_and_slide
should only happen during physics, so change _process
to _physics_process
.
The direction could be massively simplified with Input.get_vector
and using basis
which I assume you were trying to do but the forward/right vectors don’t rotate like the basis
does on it’s own.
func _physics_process(delta: float) -> void:
var input_direction: Vector2 = Input.get_vector("left", "right", "forward", "backward")
var direction: Vector3 = head.basis * Vector3(input_direction.x, 0, input_direction.y)
# you should use a seperate horizontal/vertical velocity so you can preserve gravity
var vertical_velocity := velocity.project(up_direction)
var horizontal_velocity := velocity - vertical_velocity
# assign player movement on a horizontal plane, not overwriting gravity
horizontal_velocity = direction * playerSpeeed
# gravity and jumping, using vertical_velocity
if Input.is_action_just_pressed("jump") and is_on_floor():
vertical_velocity.y += jumpForce
else:
vertical_velocity.y -= gravity * delta
# re-combine horizontal and vertical velocity
velocity = vertical_velocity + horizontal_velocity
move_and_slide()
Make sure to paste code with proper formatting between three ticks ```