A delightful request! Here's a simple yet realistic 3D player movement script in GDScript for Godot 4.5 beta. This script includes:
- Realistic walking speed (no sprinting)
- View swaying ( subtle camera movement when strafing/turning)
- View bobbing (up/down camera movement when walking)
- Head tilt ( slight tilt when turning)
- Footsteps (optional, but I'll include a basic implementation)
Create a new script (e.g., PlayerMovement.gd) and attach it to your Player node (which should have a CharacterBody3D and a Camera3D as a child). Adjust the variables in the first section to your liking.
extends CharacterBody3D
# **Movement Settings**
const WALK_SPEED = 4.5 # meters per second ( realistic walking speed)
const ACCELERATION = 10.0 # speed at which player accelerates/decelerates
const FRICTION = 12.0 # friction applied when not accelerating
const MAX_TURN_RATE = 5.0 # maximum turn rate (radians per second)
# **View Effects Settings**
const VIEW_SWAY_AMOUNT = 0.05 # camera sway amplitude (higher = more sway)
const VIEW_BOBBING_AMOUNT = 0.08 # camera bobbing amplitude
const VIEW_BOBBING_SPEED = 12.0 # speed at which bobbing occurs
const HEAD_TILT_AMOUNT = 0.02 # head tilt amplitude
# **References**
@onready var camera = $Camera3D
@onready var camera_pivot = $CameraPivot # create a new Node3D as a child of Player, name it "CameraPivot" and move Camera3D inside it
# **State Variables**
var velocity = Vector3.ZERO
var acceleration_vector = Vector3.ZERO
var yaw = 0.0 # horizontal rotation
var pitch = 0.0 # vertical rotation (limited to avoid gimbal lock)
var is_walking = false
var footsteps_cooldown = 0.0 # for footsteps sound (optional)
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) # capture mouse cursor
func _process(delta):
# **Footsteps** (optional, you'll need to load a footsteps sound resource)
if is_walking and footsteps_cooldown <= 0.0:
# $FootstepsSound.play() # uncomment when you have a FootstepsSound player
footsteps_cooldown = 0.45 # adjust to your footsteps sound length
footsteps_cooldown -= delta
func _physics_process(delta):
# **Input Handling**
var input_vector = get_input_vector()
# **Movement**
acceleration_vector = get_acceleration_vector(input_vector)
apply_movement(acceleration_vector, delta)
# **View Swaying & Bobbing**
sway_camera(input_vector, delta)
bob_camera(delta)
# **Head Tilt**
tilt_head(input_vector, delta)
# **Rotation (yaw/pitch)**
handle_rotation(delta)
# **Update is_walking state**
is_walking = velocity.length() > 0.1
func get_input_vector():
var input_vector = Vector3.ZERO
if Input.is_action_pressed("ui_right") or Input.is_key_pressed(KEY_D):
input_vector.x += 1
if Input.is_action_pressed("ui_left") or Input.is_key_pressed(KEY_A):
input_vector.x -= 1
if Input.is_action_pressed("ui_up") or Input.is_key_pressed(KEY_W):
input_vector.z -= 1
if Input.is_action_pressed("ui_down") or Input.is_key_pressed(KEY_S):
input_vector.z += 1
input_vector = input_vector.normalized() # prevent faster diagonal movement
return input_vector
func get_acceleration_vector(input_vector):
var acceleration = Vector3.ZERO
if input_vector != Vector3.ZERO:
acceleration = transform.basis * Vector3(input_vector.x, 0, input_vector.z)
return acceleration
func apply_movement(acceleration_vector, delta):
if acceleration_vector != Vector3.ZERO:
velocity = velocity.lerp(acceleration_vector * WALK_SPEED, ACCELERATION * delta)
else:
velocity = velocity.lerp(Vector3.ZERO, FRICTION * delta)
set_velocity(velocity)
move_and_slide()
velocity = get_velocity() # update velocity after move_and_slide()
func sway_camera(input_vector, delta):
var sway_amount = VIEW_SWAY_AMOUNT * (input_vector.x * -1.0) # inverse X for natural feel
camera_pivot.rotation.z = lerp(camera_pivot.rotation.z, sway_amount, 10.0 * delta)
func bob_camera(delta):
if is_walking:
var bobbing = sin(Time.get_ticks_msec() * 0.001 * VIEW_BOBBING_SPEED) * VIEW_BOBBING_AMOUNT
camera_pivot.rotation.x = lerp(camera_pivot.rotation.x, bobbing * 0.5, 15.0 * delta)
camera_pivot.translation.y = lerp(camera_pivot.translation.y, 0.05 + (bobbing * 0.5), 20.0 * delta)
else:
camera_pivot.rotation.x = lerp(camera_pivot.rotation.x, 0.0, 10.0 * delta)
camera_pivot.translation.y = lerp(camera_pivot.translation.y, 0.05, 10.0 * delta)
func tilt_head(input_vector, delta):
var tilt = 0.0
if input_vector.x > 0.1:
tilt = -HEAD_TILT_AMOUNT
elif input_vector.x < -0.1:
tilt = HEAD_TILT_AMOUNT
camera_pivot.rotation.y = lerp(camera_pivot.rotation.y, tilt, 8.0 * delta)
func handle_rotation(delta):
# **Yaw (horizontal rotation)**
yaw += Input.get_action_strength("ui_right") * -MAX_TURN_RATE * delta
yaw += Input.get_action_strength("ui_left") * MAX_TURN_RATE * delta
rotation.y = lerp_angle(rotation.y, yaw, 12.0 * delta)
# **Pitch (vertical rotation, limited to avoid gimbal lock)**
pitch += Input.get_action_strength("ui_up") * -MAX_TURN_RATE * delta
pitch += Input.get_action_strength("ui_down") * MAX_TURN_RATE * delta
pitch = clamp(pitch, -1.4, 1.4) # limit pitch to avoid flipping
camera.rotation.x = lerp_angle(camera.rotation.x, pitch, 12.0 * delta)
func _input(event):
if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
yaw -= event.relative.x * 0.01 * MAX_TURN_RATE
pitch -= event.relative.y * 0.01 * MAX_TURN_RATE
pitch = clamp(pitch, -1.4, 1.4)
Node Structure:
- Player (
CharacterBody3D )
- CameraPivot (
Node3D )
- CollisionShape3D (your player's collision)
Input Actions (Project Settings > Input Map):
Ensure you have these actions defined:
ui_up
ui_down
ui_left
ui_right
You can replace KEY_W, KEY_A, KEY_S, KEY_D with your desired keys or actions.
Tips & Adjustments:
- For a more pronounced view bobbing, increase
VIEW_BOBBING_AMOUNT.
- If movement feels too slippery, increase
FRICTION.
- Adjust
ACCELERATION and WALK_SPEED to fine-tune the feel of starting/stopping movement.
- Play with
MAX_TURN_RATE for mouse sensitivity.
- If using footsteps, adjust
footsteps_cooldown to match your sound's length.
This script provides a solid foundation for realistic 3D walking mechanics. Godot 4.5's physics engine does most of the heavy lifting, making the movement feel responsive yet grounded.
Have fun tweaking and enjoy your realistic player movement!