added controls
controlls are backwards though :P
This commit is contained in:
@@ -2,8 +2,10 @@ 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
|
||||
|
||||
@@ -22,23 +24,28 @@ func _physics_process(delta):
|
||||
if not is_on_floor():
|
||||
velocity.y -= gravity * delta
|
||||
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
if Input.is_action_just_pressed("jump") and is_on_floor():
|
||||
velocity.y = jump_velocity
|
||||
|
||||
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()
|
||||
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 = sprint_speed if Input.is_key_pressed(KEY_SHIFT) else speed
|
||||
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
|
||||
|
||||
# Rotate player to face movement direction
|
||||
var look_dir = Vector3(direction.x, 0, direction.z)
|
||||
if look_dir.length() > 0:
|
||||
look_at(global_position + look_dir, Vector3.UP)
|
||||
# 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()
|
||||
move_and_slide()
|
||||
|
||||
Reference in New Issue
Block a user