Add fallback camera with mouse controls
This commit is contained in:
parent
2824f77450
commit
ad4bb45951
6 changed files with 79 additions and 1 deletions
58
project/aircraft/fallback_input.gd
Normal file
58
project/aircraft/fallback_input.gd
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
# Based on Godot Engine Third Person Shooter Demo, used under MIT License
|
||||
# Copyright (c) 2018-2021 Juan Linietsky, Godot Engine contributors
|
||||
# https://github.com/godotengine/tps-demo/blob/master/player/player_input.gd
|
||||
|
||||
extends Node3D
|
||||
|
||||
const CAMERA_MOUSE_ROTATION_SPEED := 0.001
|
||||
const CAMERA_X_ROT_MIN := deg_to_rad(-85)
|
||||
const CAMERA_X_ROT_MAX := deg_to_rad(70)
|
||||
|
||||
var active: bool = false
|
||||
var initial_rotation: Vector3
|
||||
|
||||
@onready var camera_base = $"."
|
||||
@onready var camera = $FallbackCamera
|
||||
|
||||
func _ready() -> void:
|
||||
set_process(false)
|
||||
set_process_input(false)
|
||||
initial_rotation = camera.rotation
|
||||
|
||||
func _on_aircraft_use_fallback(active_in: bool) -> void:
|
||||
if active_in:
|
||||
set_process(true)
|
||||
set_process_input(true)
|
||||
camera.make_current()
|
||||
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
||||
else:
|
||||
set_process(false)
|
||||
set_process_input(false)
|
||||
camera.current = false
|
||||
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) # default value
|
||||
|
||||
# Reset the rotation
|
||||
camera_base.rotation = Vector3.ZERO
|
||||
camera.rotation = initial_rotation
|
||||
|
||||
active = active_in
|
||||
|
||||
|
||||
func _input(event):
|
||||
# Make mouse aiming speed resolution-independent
|
||||
# (required when using the `canvas_items` stretch mode).
|
||||
var scale_factor: float = min(
|
||||
(float(get_viewport().size.x) / get_viewport().get_visible_rect().size.x),
|
||||
(float(get_viewport().size.y) / get_viewport().get_visible_rect().size.y)
|
||||
)
|
||||
|
||||
if event is InputEventMouseMotion:
|
||||
var camera_speed_this_frame = CAMERA_MOUSE_ROTATION_SPEED
|
||||
rotate_camera(event.relative * camera_speed_this_frame * scale_factor)
|
||||
|
||||
|
||||
func rotate_camera(move):
|
||||
camera_base.rotate_y(-move.x)
|
||||
# After relative transforms, camera needs to be renormalized.
|
||||
camera_base.orthonormalize()
|
||||
camera.rotation.x = clamp(camera.rotation.x - move.y, CAMERA_X_ROT_MIN, CAMERA_X_ROT_MAX)
|
||||
Loading…
Add table
Add a link
Reference in a new issue