Files
Flight-Deck/Assets/Scripts/InstrumentManager.cs

101 lines
3.0 KiB
C#

using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;
public class InstrumentManager : MonoBehaviour
{
[Header("Input")]
public InputActionReference throttleAction; // Your Input Action reference
public InputActionReference attitudeAction; // Vector3 input for pitch/yaw/roll control
[Header("Throttle Settings")]
public float rampSpeed = 1f; // Units per second
[Header("Attitude Control Settings")]
public float attitudeResponseSpeed = 1f; // Multiplier for attitude input sensitivity
[Header("References")]
public MainEngine mainEngine; // Reference to your engine script
public GimbalSystem gimbalSystem; // Reference to gimbal control system
// Referance to TextMeshPro Throttle slider
public Slider throttleDisplay; // Reference to UI Slider component
void OnEnable()
{
if (throttleAction != null)
{
throttleAction.action.Enable();
Debug.Log("Throttle action enabled.");
}
else
{
Debug.LogWarning("Throttle action reference is not set!");
}
if (attitudeAction != null)
{
attitudeAction.action.Enable();
Debug.Log("Attitude action enabled.");
}
else
{
Debug.LogWarning("Attitude action reference is not set!");
}
}
void OnDisable()
{
if (throttleAction != null)
throttleAction.action.Disable();
if (attitudeAction != null)
attitudeAction.action.Disable();
}
void Update()
{
// Handle throttle input
if (throttleAction == null || mainEngine == null)
return;
// Read input (-1, 0, +1)
float input = throttleAction.action.ReadValue<float>();
// Debug what the input is reading
//Debug.Log($"Input value: {input}");
// Calculate new throttle target
float target = mainEngine.throttleInput + input * rampSpeed * Time.deltaTime;
// Clamp between 0 and 1
mainEngine.throttleInput = Mathf.Clamp01(target);
// Debug the throttle value
//Debug.Log($"Throttle value: {mainEngine.throttleInput}");
// Update the throttle display
if (throttleDisplay != null)
{
throttleDisplay.value = mainEngine.throttleInput;
}
// Handle attitude control input
if (attitudeAction != null && gimbalSystem != null)
{
// Read Vector3 input
Vector3 attitudeInput = attitudeAction.action.ReadValue<Vector3>();
// Apply attitude response speed multiplier
attitudeInput *= attitudeResponseSpeed;
// Map inputs to gimbal system:
// Y (up/down) → Pitch
// X (left/right) → Yaw
// Z (forward/backward) → Roll
gimbalSystem.SetPitchInput(attitudeInput.y);
gimbalSystem.SetYawInput(attitudeInput.x);
gimbalSystem.SetRollInput(attitudeInput.z);
}
}
}