mirror of
https://github.com/FOSS-Supremacy/OpenLiberty.git
synced 2025-04-28 11:57:58 +03:00
feat: VehicleCameraController script
This commit is contained in:
parent
0fc94b2fd2
commit
5b56933e90
1 changed files with 26 additions and 16 deletions
|
@ -1,13 +1,16 @@
|
|||
# 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.
|
||||
# by: a6xdev
|
||||
# by: a6xdev, ...
|
||||
|
||||
# <-- Still need a way to check when the car is reversing. -->
|
||||
# <-- This script will undergo a reformulation and will become a global camera. Do not make unnecessary additions. -->
|
||||
|
||||
extends Node3D
|
||||
|
||||
@export var Vehicle: VehicleBody3D
|
||||
@export var VehicleEngineNode:VehicleEngine
|
||||
|
||||
@export_group("Camera Settings")
|
||||
@export var FreeCamera:bool = false ## Camera livre para ajudar na analise e perfomace do Veiculo.
|
||||
@export var ControllerState: CameraState
|
||||
|
||||
enum CameraState {
|
||||
|
@ -17,9 +20,9 @@ enum CameraState {
|
|||
|
||||
@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.
|
||||
@export var smooth_time: float = 0.05 ## Camera transition time.
|
||||
@export var smooth_time: float = 0.07 ## Camera transition time.
|
||||
@export var mouse_sens: float = 0.4 ## Mouse sensitivity
|
||||
@export var max_steering_rotation: float = 2.0 ## Maximum camera rotation based on direction
|
||||
@export var max_steering_rotation: float = 10.0 ## Maximum camera rotation based on direction
|
||||
|
||||
var car_is_moving: bool = false
|
||||
var pivot: Node3D
|
||||
|
@ -41,20 +44,24 @@ func _input(event):
|
|||
func _process(delta):
|
||||
var target_rotation = forward_rotation
|
||||
|
||||
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
|
||||
if not FreeCamera:
|
||||
if is_reversing(): # If you are reversing, the camera rotates in reverse, prioritizing the front of the car.
|
||||
# <-- Desativado por motivos de bugs muito estranhos --> NÂO TIRE O COMENTARIO DESSA BOMBA
|
||||
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
|
||||
|
||||
# Smooths camera rotation forward or backward
|
||||
pivot.rotation_degrees = lerp(pivot.rotation_degrees, target_rotation, smooth_time)
|
||||
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
|
||||
var steering_rotation = Vehicle.steering * max_steering_rotation
|
||||
pivot.rotation_degrees.y += steering_rotation # Adiciona a rotação lateral
|
||||
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
|
||||
match ControllerState:
|
||||
|
@ -66,5 +73,8 @@ func _process(delta):
|
|||
# Checks if the car is reversing.
|
||||
# Still doesn't work very well.
|
||||
func is_reversing() -> bool:
|
||||
var is_reverse_input = Input.is_action_pressed("car_break")
|
||||
return is_reverse_input and Vehicle.linear_velocity.z > 0
|
||||
var test = Vehicle.linear_velocity.dot(Vehicle.global_transform.basis.z)
|
||||
if test < 0:
|
||||
return true
|
||||
else:
|
||||
return false
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue