could you write me a simple gdscript for godot beta 4.5 with 3d player movement please?
make it realistic like view swaying, view bobbing, you understand. also no sprinting, just realistic walking, but it shouldnt be like buttery smooth, realistic but playable
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a realistic 3D player movement script for Godot 4.5:
gdscript
extends CharacterBody3D
@export var walk_speed: float = 3.0
@export var mouse_sensitivity: float = 0.002
@export var jump_velocity: float = 6.0
# Head bob variables
@export var bob_frequency: float = 2.0
@export var bob_amplitude: float = 0.08
@export var bob_lerp_speed: float = 10.0
# View sway variables
@export var sway_amount: float = 0.02
@export var sway_speed: float = 8.0
# Movement smoothing
@export var acceleration: float = 8.0
@export var friction: float = 12.0
@onready var head: Node3D = $Head
@onready var camera: Camera3D = $Head/Camera3D
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
var bob_time: float = 0.0
var default_camera_pos: Vector3
var input_vector: Vector2
var sway_target: Vector3
func _ready():
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
default_camera_pos = camera.position
func _input(event):
if event is InputEventMouseMotion:
# Rotate head horizontally
head.rotate_y(-event.relative.x * mouse_sensitivity)
# Rotate camera vertically with clamping
camera.rotate_x(-event.relative.y * mouse_sensitivity)
camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-80), deg_to_rad(80))
func _physics_process(delta):
handle_gravity(delta)
handle_jump()
handle_movement(delta)
handle_head_bob(delta)
handle_view_sway(delta)
move_and_slide()
func handle_gravity(delta):
if not is_on_floor():
velocity.y -= gravity * delta
func handle_jump():
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = jump_velocity
func handle_movement(delta):
input_vector = Vector2.ZERO
if Input.is_action_pressed("move_right"):
input_vector.x += 1
if Input.is_action_pressed("move_left"):
input_vector.x -= 1
if Input.is_action_pressed("move_back"):
input_vector.y += 1
if Input.is_action_pressed("move_forward"):
input_vector.y -= 1
input_vector = input_vector.normalized()
# Get movement direction relative to head rotation
var direction = (head.transform.basis * Vector3(input_vector.x, 0, input_vector.y)).normalized()
if direction != Vector3.ZERO:
# Apply acceleration
velocity.x = move_toward(velocity.x, direction.x * walk_speed, acceleration * delta)
velocity.z = move_toward(velocity.z, direction.z * walk_speed, acceleration * delta)
else:
# Apply friction
velocity.x = move_toward(velocity.x, 0, friction * delta)
velocity.z = move_toward(velocity.z, 0, friction * delta)
func handle_head_bob(delta):
if is_on_floor() and input_vector.length() > 0:
bob_time += delta * bob_frequency
var bob_offset = Vector3.ZERO
bob_offset.y = sin(bob_time) * bob_amplitude
bob_offset.x = sin(bob_time * 0.5) * bob_amplitude * 0.3
camera.position = camera.position.lerp(default_camera_pos + bob_offset, bob_lerp_speed * delta)
else:
camera.position = camera.position.lerp(default_camera_pos, bob_lerp_speed * delta)
func handle_view_sway(delta):
# Calculate sway based on input
sway_target.x = -input_vector.x * sway_amount
sway_target.z = input_vector.y * sway_amount * 0.5
# Apply sway to camera rotation
var current_sway = Vector3(
camera.rotation.z,
0,
camera.rotation.y
)
var new_sway = current_sway.lerp(sway_target, sway_speed * delta)
camera.rotation.z = new_sway.x
# Don't modify camera.rotation.y as it's controlled by mouse look
You'll need to set up the input map in Godot with these actions:
move_forward (W key)
move_back (S key)
move_left (A key)
move_right (D key)
ui_accept (Space for jumping)
The scene structure should be:
code
CharacterBody3D (with this script)
├── CollisionShape3D (with a CapsuleShape3D)
└── Head (Node3D)
└── Camera3D
This gives you realistic movement with subtle head bobbing when walking, view sway when turning, and momentum-based acceleration/deceleration rather than instant movement. The movement feels grounded but still responsive for gameplay.