Player camera controls

This commit is contained in:
Vojtěch Struhár 2025-02-10 23:52:19 +01:00
parent 0c12992ba4
commit 7efb6d3e84
2 changed files with 43 additions and 13 deletions

View File

@ -1,13 +1,15 @@
extends CharacterBody3D extends CharacterBody3D
@onready var camera: Camera3D = $Camera3D
const SPEED = 5.0 const SPEED = 5.0
const JUMP_VELOCITY = 4.5 const JUMP_VELOCITY = 4.5
const MOUSE_SENSITIVITY = 0.01 const MOUSE_SENSITIVITY = 0.005
func _ready() -> void: func _ready() -> void:
Input.mouse_mode = Input.MOUSE_MODE_CONFINED_HIDDEN Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
func _process(delta: float) -> void: func _process(delta: float) -> void:
if Input.is_action_just_pressed("ui_cancel"): if Input.is_action_just_pressed("ui_cancel"):
@ -15,7 +17,9 @@ func _process(delta: float) -> void:
func _unhandled_input(event: InputEvent) -> void: func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseMotion: if event is InputEventMouseMotion:
rotate_y(-event.relative.x * MOUSE_SENSITIVITY) rotate_y(-event.screen_relative.x * MOUSE_SENSITIVITY)
camera.rotate_x(-event.screen_relative.y * MOUSE_SENSITIVITY)
camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-80), deg_to_rad(80))
func _physics_process(delta: float) -> void: func _physics_process(delta: float) -> void:
# Add the gravity. # Add the gravity.
@ -26,15 +30,17 @@ func _physics_process(delta: float) -> void:
if Input.is_action_just_pressed("ui_accept") and is_on_floor(): if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions. var right:Vector3 = (global_transform.basis.x * Vector3(1, 0, 1)).normalized()
var input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down") var forward:Vector3 = (global_transform.basis.z * Vector3(1, 0, 1)).normalized()
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction: if Input.is_key_pressed(KEY_LEFT) or Input.is_key_pressed(KEY_A):
velocity.x = direction.x * SPEED global_translate(-right * SPEED * delta)
velocity.z = direction.z * SPEED if Input.is_key_pressed(KEY_RIGHT) or Input.is_key_pressed(KEY_D):
else: global_translate(right * SPEED * delta)
velocity.x = move_toward(velocity.x, 0, SPEED) if Input.is_key_pressed(KEY_UP) or Input.is_key_pressed(KEY_W):
velocity.z = move_toward(velocity.z, 0, SPEED) global_translate(-forward * SPEED * delta)
if Input.is_key_pressed(KEY_DOWN) or Input.is_key_pressed(KEY_S):
global_translate(forward * SPEED * delta)
move_and_slide() move_and_slide()

View File

@ -11,5 +11,29 @@ config_version=5
[application] [application]
config/name="DP Konzultace" config/name="DP Konzultace"
run/main_scene="uid://dpatwnepecl8r"
config/features=PackedStringArray("4.4", "Forward Plus") config/features=PackedStringArray("4.4", "Forward Plus")
config/icon="res://icon.svg" config/icon="res://icon.svg"
[input]
move_left={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":65,"key_label":0,"unicode":97,"location":0,"echo":false,"script":null)
]
}
move_right={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":68,"key_label":0,"unicode":100,"location":0,"echo":false,"script":null)
]
}
move_forward={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":87,"key_label":0,"unicode":119,"location":0,"echo":false,"script":null)
]
}
move_back={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":115,"location":0,"echo":false,"script":null)
]
}