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?