cleaned up the code more.
This commit is contained in:
30
scripts/player.gd
Normal file
30
scripts/player.gd
Normal file
@@ -0,0 +1,30 @@
|
||||
extends CharacterBody3D
|
||||
|
||||
@export var speed = 5.0
|
||||
@export var jump_velocity = 4.5
|
||||
|
||||
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
|
||||
|
||||
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
|
||||
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()
|
||||
|
||||
if direction:
|
||||
# Rotate to face movement direction
|
||||
look_at(position + direction, Vector3.UP)
|
||||
velocity.x = direction.x * speed
|
||||
velocity.z = direction.z * speed
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, speed)
|
||||
velocity.z = move_toward(velocity.z, 0, speed)
|
||||
|
||||
move_and_slide()
|
||||
1
scripts/player.gd.uid
Normal file
1
scripts/player.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://c2oh06p4hqbjv
|
||||
@@ -1,51 +0,0 @@
|
||||
extends CharacterBody3D
|
||||
|
||||
@export var speed = 5.0
|
||||
@export var sprint_speed = 10.0
|
||||
@export var crouch_speed = 2.0
|
||||
@export var jump_velocity = 4.5
|
||||
@export var mouse_sensitivity = 0.002
|
||||
@export var turn_speed = 10.0
|
||||
|
||||
@onready var camera = $Camera3D
|
||||
|
||||
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
|
||||
|
||||
func _ready():
|
||||
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
|
||||
|
||||
func _input(event):
|
||||
if event is InputEventMouseMotion:
|
||||
rotate_y(-event.relative.x * mouse_sensitivity)
|
||||
camera.rotate_x(-event.relative.y * mouse_sensitivity)
|
||||
camera.rotation.x = clamp(camera.rotation.x, -PI/2, PI/2)
|
||||
|
||||
func _physics_process(delta):
|
||||
if not is_on_floor():
|
||||
velocity.y -= gravity * delta
|
||||
|
||||
if Input.is_action_just_pressed("jump") and is_on_floor():
|
||||
velocity.y = jump_velocity
|
||||
|
||||
var input_dir = Input.get_vector("slideLeft", "slideRight", "backward", "forward")
|
||||
input_dir = -input_dir
|
||||
var direction = (camera.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
|
||||
|
||||
if direction:
|
||||
var current_speed = speed
|
||||
if Input.is_action_pressed("sprint"):
|
||||
current_speed = sprint_speed
|
||||
elif Input.is_action_pressed("crouch"):
|
||||
current_speed = crouch_speed
|
||||
velocity.x = direction.x * current_speed
|
||||
velocity.z = direction.z * current_speed
|
||||
|
||||
# Smoothly rotate player to face movement direction only if moving forward/backward
|
||||
if input_dir.y != 0:
|
||||
var target_angle = atan2(direction.x, direction.z)
|
||||
rotation.y = lerp_angle(rotation.y, target_angle, delta * turn_speed)
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, speed)
|
||||
velocity.z = move_toward(velocity.z, 0, speed)
|
||||
|
||||
move_and_slide()
|
||||
@@ -1 +0,0 @@
|
||||
uid://cyqtabd2e7m8d
|
||||
Reference in New Issue
Block a user