OpenLiberty/scripts/systems/VehicleCameraController.gd

83 lines
2.8 KiB
GDScript3
Raw Permalink Normal View History

# Code still under development and there may still be small bugs.
# Don't try to ask me how this works, because even I don't know.
2024-08-24 10:12:02 -03:00
# by: a6xdev, ...
2024-08-24 10:12:02 -03:00
# <-- This script will undergo a reformulation and will become a global camera. Do not make unnecessary additions. -->
2024-08-18 11:57:45 -03:00
extends Node3D
@export var Vehicle: VehicleBody3D
2024-08-24 10:12:02 -03:00
@export var VehicleEngineNode:VehicleEngine
@export_group("Camera Settings")
2024-08-24 10:12:02 -03:00
@export var FreeCamera:bool = false ## Camera livre para ajudar na analise e perfomace do Veiculo.
2024-08-18 11:57:45 -03:00
@export var ControllerState: CameraState
2024-08-18 11:57:45 -03:00
enum CameraState {
ACTIVE,
DISABLE
}
@export var forward_rotation: Vector3 = Vector3(-15, 0, 0) ## Rotation prioritizing the rear of the car.
@export var reverse_rotation: Vector3 = Vector3(-15, 180, 0) ## Rotation prioritizing the front of the car.
2024-08-24 10:12:02 -03:00
@export var smooth_time: float = 0.07 ## Camera transition time.
@export var mouse_sens: float = 0.4 ## Mouse sensitivity
2024-08-24 10:12:02 -03:00
@export var max_steering_rotation: float = 10.0 ## Maximum camera rotation based on direction
var car_is_moving: bool = false
var pivot: Node3D
var camera3d: Camera3D
var spring_arm3d: SpringArm3D
2024-08-18 11:57:45 -03:00
func _ready():
pivot = $Pivot
camera3d = $Pivot/SpringArm3D/Camera3D
spring_arm3d = $Pivot/SpringArm3D
2024-08-18 11:57:45 -03:00
func _input(event):
if event is InputEventMouseMotion and ControllerState == CameraState.ACTIVE and not car_is_moving:
# Rotate CameraPivot based on mouse movement
pivot.rotate_y(deg_to_rad(-event.relative.x * mouse_sens))
pivot.rotate_x(deg_to_rad(-event.relative.y * mouse_sens))
pivot.rotation.z = 0 # Resets Z rotation to avoid tilt
func _process(delta):
var target_rotation = forward_rotation
2024-08-24 10:12:02 -03:00
if not FreeCamera:
if is_reversing(): # If you are reversing, the camera rotates in reverse, prioritizing the front of the car.
target_rotation = reverse_rotation
car_is_moving = true
elif Vehicle.linear_velocity.length() > 2: # If you are going forward, the camera prioritizes the rear of the car.
car_is_moving = true
else:
car_is_moving = false
2024-08-18 11:57:45 -03:00
# Smooths camera rotation forward or backward
2024-08-24 10:12:02 -03:00
if car_is_moving:
pivot.rotation_degrees = lerp(pivot.rotation_degrees, target_rotation, smooth_time)
# Adds rotation based on the car's steering to show more of the side
2024-08-24 10:12:02 -03:00
if not FreeCamera:
var steering_rotation = Vehicle.steering * max_steering_rotation * smooth_time
pivot.rotation_degrees.y += steering_rotation # Adiciona a rotação lateral
# Enable or disable the camera based on the controller state
2024-08-18 11:57:45 -03:00
match ControllerState:
CameraState.ACTIVE:
camera3d.current = true
2024-08-18 11:57:45 -03:00
CameraState.DISABLE:
camera3d.current = false
# Checks if the car is reversing.
# Still doesn't work very well.
func is_reversing() -> bool:
2024-08-24 10:28:47 -03:00
if Vehicle.linear_velocity.length() > 2:
var test = Vehicle.linear_velocity.dot(Vehicle.global_transform.basis.z)
if test < 0:
return true
else:
return false
2024-08-24 10:12:02 -03:00
else:
return false