Added 3D Controller

This commit is contained in:
cblech
2024-12-02 18:50:26 +01:00
parent 421a90ec17
commit 4ce0367f15
5 changed files with 653 additions and 19 deletions
+22
View File
@@ -0,0 +1,22 @@
extends Node3D
@export var canPitch: bool = false
@export var canYaw: bool = false
@export var rotateSpeed: float = 0.003
@onready var sub_pivot: Node3D = $SubPivot
func _ready() -> void:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
func _input(event):
if event.is_action_pressed("click"):
if Input.mouse_mode == Input.MOUSE_MODE_VISIBLE:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
if event.is_action_pressed("ui_cancel"):
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
if event is InputEventMouseMotion:
if Input.mouse_mode != Input.MOUSE_MODE_CAPTURED: return
if canYaw: sub_pivot.rotate_x(event.relative.y * -rotateSpeed)
if canPitch: rotate_y(event.relative.x * -rotateSpeed)
+30
View File
@@ -0,0 +1,30 @@
extends CharacterBody3D
@export var SPEED = 1.0
@onready var camera_3d: Camera3D = $CameraPivot/SubPivot/Camera3D
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
#if Input.is_action_just_pressed("ui_accept") and is_on_floor():
# 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 input_dir := Input.get_vector("move_left", "move_right", "move_up", "move_down")
input_dir = input_dir.rotated(-camera_3d.global_rotation.y)
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
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()