There's only one script within the scene, for the player.
var speed
const WALK_SPEED = 5.0
const SPRINT_SPEED = 7.5
const JUMP_VELOCITY = 4.5
const SENSITIVITY = 0.003
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = 9.8
@onready var head = $Head
@onready var camera = $Head/PlayerPOV
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _unhandled_input(event):
if event is InputEventMouseMotion:
head.rotate_y(-event.relative.x * SENSITIVITY)
camera.rotate_x(-event.relative.y * SENSITIVITY)
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y -= gravity * delta
# Handle Jump.
if Input.is_action_just_pressed("move_jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Handle Sprint.
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("move_right", "move_left", "move_backwards", "move_forward")
var direction =- (head.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if is_on_floor():
if direction:
velocity.x = direction.x * speed
velocity.z = direction.z * speed
else:
velocity.x = lerp(velocity.x, direction.x * speed, delta * 8)
velocity.z = lerp(velocity.z, direction.z * speed, delta * 8)
else:
velocity.x = lerp(velocity.x, direction.x * speed, delta * 3.5)
velocity.z = lerp(velocity.z, direction.z * speed, delta * 3.5)
move_and_slide()```