Here's a realistic 3D player movement script for Godot 4.5:
extends CharacterBody3D
# Movement settings
const WALK_SPEED = 3.5
const ACCELERATION = 8.0
const FRICTION = 12.0
const GRAVITY = 9.8
# Camera settings
const MOUSE_SENSITIVITY = 0.002
const CAMERA_SMOOTHING = 15.0
# View bobbing settings
const BOB_FREQUENCY = 2.2
const BOB_AMPLITUDE = 0.06
const BOB_SWAY_AMPLITUDE = 0.04
# View sway settings
const SWAY_AMOUNT = 0.02
const SWAY_SPEED = 8.0
const SWAY_RECOVERY = 6.0
# Node references
@onready var camera_pivot = $CameraPivot
@onready var camera = $CameraPivot/Camera3D
# State variables
var camera_rotation = Vector2.ZERO
var bob_time = 0.0
var velocity_smoothed = Vector3.ZERO
var sway_offset = Vector2.ZERO
var sway_velocity = Vector2.ZERO
var is_moving = false
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _input(event):
if event is InputEventMouseMotion:
camera_rotation.x -= event.relative.x * MOUSE_SENSITIVITY
camera_rotation.y -= event.relative.y * MOUSE_SENSITIVITY
camera_rotation.y = clamp(camera_rotation.y, -1.4, 1.4)
# Add sway based on mouse movement
sway_velocity = event.relative * SWAY_AMOUNT
func _physics_process(delta):
# Handle gravity
if not is_on_floor():
velocity.y -= GRAVITY * delta
else:
velocity.y = -0.1 # Small downward force to keep grounded
# Get input direction
var input_dir = Vector2()
input_dir.x = Input.get_axis("move_left", "move_right")
input_dir.y = Input.get_axis("move_forward", "move_back")
input_dir = input_dir.normalized()
# Calculate movement direction relative to camera
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
# Apply movement with acceleration
if direction.length() > 0:
velocity.x = lerp(velocity.x, direction.x * WALK_SPEED, ACCELERATION * delta)
velocity.z = lerp(velocity.z, direction.z * WALK_SPEED, ACCELERATION * delta)
is_moving = true
else:
velocity.x = lerp(velocity.x, 0.0, FRICTION * delta)
velocity.z = lerp(velocity.z, 0.0, FRICTION * delta)
is_moving = false
# Smooth velocity for bobbing calculations
velocity_smoothed = velocity_smoothed.lerp(velocity, 10.0 * delta)
# Apply movement
move_and_slide()
# Update camera
_update_camera(delta)
func _update_camera(delta):
# Apply camera rotation
camera_pivot.rotation.y = camera_rotation.x
camera_pivot.rotation.x = camera_rotation.y
# Calculate view bobbing
var bob_offset = Vector3.ZERO
if is_moving and is_on_floor():
bob_time += delta * velocity_smoothed.length() * BOB_FREQUENCY
bob_offset.y = sin(bob_time) * BOB_AMPLITUDE
bob_offset.x = cos(bob_time * 0.5) * BOB_SWAY_AMPLITUDE
else:
bob_time = lerp(bob_time, 0.0, 5.0 * delta)
# Apply view sway
sway_offset = sway_offset.lerp(-sway_velocity, SWAY_SPEED * delta)
sway_velocity = sway_velocity.lerp(Vector2.ZERO, SWAY_RECOVERY * delta)
# Apply all camera effects
camera.position = camera.position.lerp(bob_offset, CAMERA_SMOOTHING * delta)
camera.rotation.z = lerp(camera.rotation.z, sway_offset.x * 0.5, CAMERA_SMOOTHING * delta)
camera.rotation.x = lerp(camera.rotation.x, sway_offset.y * 0.3, CAMERA_SMOOTHING * delta)
func _unhandled_input(event):
if event.is_action_pressed("ui_cancel"):
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
else:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
To use this script:
- Create a CharacterBody3D node as your player
- Add a Node3D child called "CameraPivot"
- Add a Camera3D as a child of CameraPivot
- Position the CameraPivot at about Y = 1.7 (eye height)
- Attach this script to the CharacterBody3D
You'll also need to set up the input actions in Project Settings:
- "move_forward" (W key)
- "move_back" (S key)
- "move_left" (A key)
- "move_right" (D key)
The script includes:
- Realistic walking speed with acceleration/deceleration
- View bobbing that responds to movement speed
- Subtle horizontal sway during walking
- Mouse-look sway when turning quickly
- Smooth but not overly smooth camera movement
- Gravity and proper ground detection
The movement feels weighty and grounded rather than floaty, with just enough camera effects to feel immersive without being nauseating.