Godot Version
4.2.2
Question
I’m following the “First 3D game” tutorial from the docs.
I’ve encountered a problem in the “Moving the player with code” section.
As the title suggests, whatever I do I can’t get the player to move.
Here’s the code:
extends CharacterBody3D
@export var speed = 14
@export var fall_acceleration = 75
var target_velocity = Vector3.ZERO
func _physics_process(delta):
var direction = Vector3.ZERO
if Input.is_action_pressed("move_right"):
direction.x += 1
if Input.is_action_pressed("move_left"):
direction.x -= 1
if Input.is_action_pressed("move_forward"):
direction.z += 1
if Input.is_action_pressed("move_back"):
direction.z -= 1
if direction != Vector3.ZERO:
direction = direction.normalized()
$Pivot.basis = Basis.looking_at(direction)
target_velocity.x = direction.x * speed
target_velocity.z = direction.z * speed
if not is_on_floor():
target_velocity.y = target_velocity.y - (fall_acceleration * delta)
velocity = target_velocity
# velocity = Vector3.ONE
move_and_slide()
I know the inputs are working because the model changes direction.
I also tried setting velocity to a vector of ones but that didn’t fix it either.
The script is pretty much a copy of the code in the tutorial, but it doesn’t work.
Any help would be much appreciated.