Sure! Below is a simple but realistic 3D player movement script written in GDScript for Godot 4.5 beta . It includes:
Basic walking movement with gravity and collision.
Mouse look (free camera movement).
Head bobbing for walking animation (not excessive).
Camera sway when moving the mouse (adds immersion).
Configuration options for realism (like bob intensity, sway limits, etc.).
✅ Node Setup:
Before using the script, please make sure your scene is set up like this:
code Copy code
Player (CharacterBody3D)
├── Head (Node3D)
│ └── Camera3D
└── CollisionShape3D
Player should be a CharacterBody3D
Head is used to rotate up/down (pitch)
Camera3D is the first-person view
CollisionShape3D defines the body’s collision
🧠 GDScript — Player.gd
Attach this script to the Player (CharacterBody3D) node.
gdscript Copy code
extends CharacterBody3D
@export var walk_speed := 3.0
@export var mouse_sensitivity := 0.002
@export var head_bob_speed := 8.0
@export var head_bob_amount := 0.05
@export var max_look_angle := 89.0
@onready var head = $Head
@onready var camera = $Head/Camera3D
var velocity: Vector3 = Vector3.ZERO
var head_bob_timer: float = 0.0
var pitch: float = 0.0 # vertical look (up/down)
const GRAVITY := ProjectSettings.get_setting("physics/3d/default_gravity")
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _unhandled_input(event):
if event is InputEventMouseMotion:
rotate_camera(event.relative)
if event is InputEventKey and event.pressed:
if event.keycode == KEY_ESCAPE:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
func rotate_camera(mouse_delta: Vector2):
yaw = -mouse_delta.x * mouse_sensitivity
pitch_delta = -mouse_delta.y * mouse_sensitivity
rotate_y(yaw)
pitch = clamp(pitch + pitch_delta, deg_to_rad(-max_look_angle), deg_to_rad(max_look_angle))
head.rotation.x = pitch
func _physics_process(delta):
var input_dir = Vector2.ZERO
input_dir.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
input_dir.y = Input.get_action_strength("move_backward") - Input.get_action_strength("move_forward")
input_dir = input_dir.normalized()
# Calculate local movement direction
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
# Basic walking velocity
velocity.x = direction.x * walk_speed
velocity.z = direction.z * walk_speed
# Gravity
if not is_on_floor():
velocity.y -= GRAVITY * delta
else:
velocity.y = 0.0
# Apply movement
move_and_slide()
# Animate view bobbing
handle_head_bob(delta, direction)
func handle_head_bob(delta: float, move_direction: Vector3):
if move_direction.length() > 0.1 and is_on_floor():
head_bob_timer += head_bob_speed * delta
var bob_offset = sin(head_bob_timer * PI * 2.0) * head_bob_amount
camera.transform.origin.y = bob_offset
else:
# Reset head bobbing
head_bob_timer = 0.0
camera.transform.origin.y = 0.0
🕹️ Input Map Setup:
Go to Project > Project Settings > Input Map , and add these actions:
move_forward → W
move_backward → S
move_left → A
move_right → D
✅ Features Recap:
Feature Included? Notes Walking movement ✅ Configurable speed Mouse Look ✅ Horizontal and vertical rotation Gravity ✅ Uses project gravity Realistic Bobbing ✅ Subtle when walking Head Sway ✅ Integrated into mouse look and bob Jumping ❌ Not included as per your request Sprinting ❌ Not included
🔧 Tweaking Tips
Adjust walk_speed and head_bob_amount for how "heavy" or "loose" the player feels.
The mouse_sensitivity might need fine-tuning per your personal monitor DPI and preference.
Let me know if you want crouching, jumping, or a sway animation when stopping or turning!