Movement template isn't working

Godot Version

Godot_v4.6-stable_win64

Question

so I just start Godot yesterday, and wanted to make some simple first person movement, so I started watching some videos to get started. I had some prior knowledge on the coding software Lua, so i thought it wouldn’t be to difficult. But when I started using the CharacterBody3D movement script template, It started just freezing after maybe a second. I’m not sure if this is a problem with my computer or something I did with the script. I can also provide a video if needed

extends CharacterBody3D

var speed
const WALK_SPEED = 5.0
const SPRINT_SPEED = 8.0
const JUMP_VELOCITY = 4.5
const SENSITIVITY = 0.01

#head bob stuff
const BOB_FREQ = 2.0
const BOB_AMP = 0.08
var t_bob = 0.0

var gravity = 9.8

@onready var head = $NeckNode
@onready var camera = $NeckNode/Camera3D

func _unhandled_input(event: InputEvent) -> void:
	if event is InputEventMouseButton:
		Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
	elif event.is_action_pressed("ui_cancel"):
		Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
	if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
		head.rotate_y(-event.relative.x * SENSITIVITY)
		camera.rotate_x(-event.relative.y * SENSITIVITY)
		camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-40), deg_to_rad(60))

func _physics_process(delta: float) -> void:
	# Add the gravity.
	if not is_on_floor():
		velocity.y -= gravity * delta

	# Handle jump.
	if Input.is_action_just_pressed("Jump") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	#RUN
	if Input.is_action_pressed("Sprint"):
		speed = SPRINT_SPEED
	else:
		speed = WALK_SPEED

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var input_dir := Input.get_vector("Left", "Right", "Forward", "Backward")
	var direction = (head.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 = 0.0
		velocity.z = 0.0
	#headbob
	t_bob += delta * velocity.length() * float(is_on_floor())
	camera.transform.origin = _headbob(t_bob)

	move_and_slide()

func _headbob(time) -> Vector3:
	var pos = Vector3.ZERO
	pos.y = sin(time * BOB_FREQ) * BOB_AMP
	pos.x = sin(time * BOB_FREQ / 1.5) * BOB_AMP
	return pos

Also, It seems to work fine at first, but the WASD movement stops doing anything after a second from running the scene, but the camera still turns with the mouse. Also, despite checking the code and matching it with the video, the head bob and jumping doesn’t work either. Help?

1 Like

Check you input in settings , maybe you undone them ?

Like i said, the wasd movement works, until a second after running it.

If the game is freezing it may be from the debugger pointing out erroneous code. Check back in the Godot Editor for any errors and warnings in your debugger, it may even directly point out a problematic line

2 Likes

debugger says nothing wrong :frowning:

So you have other scripts attached which could be overwriting your velocity or input ?

Debugging 101: Printing.
Print something in phyiscs_process(), preferably the value of velocity

I printed the velocity, and it was showing the x and z changing, but i wasn’t moving, it also said i was constantly falling despite staying on the platform.

Can you post the video and your in-editor and runtime (remote) scene tree?

couldn’t attach mp4 file as i am a new user, does this help?