First commit
This commit is contained in:
68
scripts/Player.gd
Normal file
68
scripts/Player.gd
Normal file
@@ -0,0 +1,68 @@
|
||||
extends CharacterBody3D
|
||||
|
||||
## Simple First-Person Player Controller
|
||||
##
|
||||
## Features:
|
||||
## - WASD Movement with acceleration/deceleration
|
||||
## - Gravity
|
||||
## - Mouse Look
|
||||
##
|
||||
## Note: Modify `MOUSE_SENSITIVITY` and `SPEED` to tune gameplay.
|
||||
|
||||
# Configuration
|
||||
const SPEED = 5.0
|
||||
const SPRINT_SPEED = 8.0
|
||||
const JUMP_VELOCITY = 4.5
|
||||
const ACCELERATION = 10.0
|
||||
const FRICTION = 10.0
|
||||
const GRAVITY = 9.8
|
||||
const MOUSE_SENSITIVITY = 0.003
|
||||
|
||||
# References
|
||||
@onready var camera = $Camera3D
|
||||
|
||||
func _ready():
|
||||
# Capture mouse cursor for gameplay
|
||||
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
||||
|
||||
func _unhandled_input(event):
|
||||
# Handle Mouse Look
|
||||
if event is InputEventMouseMotion:
|
||||
rotate_y(-event.relative.x * MOUSE_SENSITIVITY)
|
||||
camera.rotate_x(-event.relative.y * MOUSE_SENSITIVITY)
|
||||
# Clamp view to prevent flipping over
|
||||
camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-90), deg_to_rad(90))
|
||||
|
||||
# Toggle Mouse Capture (Escape to exit)
|
||||
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)
|
||||
|
||||
func _physics_process(delta):
|
||||
# Add Gravity
|
||||
if not is_on_floor():
|
||||
velocity.y -= GRAVITY * delta
|
||||
|
||||
# Handle Jump
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
|
||||
# Get Input Direction (WASD / Arrows)
|
||||
# "ui_up", "ui_down", "ui_left", "ui_right" are default Godot actions
|
||||
var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
|
||||
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
|
||||
|
||||
# Determine current speed (Sprint support optional)
|
||||
var current_speed = SPRINT_SPEED if Input.is_key_pressed(KEY_SHIFT) else SPEED
|
||||
|
||||
# Apply Movement with Acceleration/Friction
|
||||
if direction:
|
||||
velocity.x = move_toward(velocity.x, direction.x * current_speed, ACCELERATION * delta)
|
||||
velocity.z = move_toward(velocity.z, direction.z * current_speed, ACCELERATION * delta)
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, FRICTION * delta)
|
||||
velocity.z = move_toward(velocity.z, 0, FRICTION * delta)
|
||||
|
||||
move_and_slide()
|
||||
Reference in New Issue
Block a user