I don't remember what I did but it was probably important
This commit is contained in:
@@ -176,6 +176,18 @@ MonoBehaviour:
|
||||
m_Script: {fileID: 11500000, guid: d943606e96a65aeceaa19bb4a56c515b, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier: '::'
|
||||
rb: {fileID: 1172921199}
|
||||
fuelTank: {fileID: 1437557826}
|
||||
rcsEnabled: 1
|
||||
consumeFuel: 1
|
||||
maxTorque: 2000
|
||||
torqueResponseRate: 6000
|
||||
leverArm: 2
|
||||
Isp: 250
|
||||
inputDeadzone: 0.05
|
||||
controlInput: {x: 0, y: 0, z: 0}
|
||||
appliedTorque: {x: 0, y: 0, z: 0}
|
||||
fuelFlowRate: 0
|
||||
--- !u!1 &109735446
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -1417,6 +1429,7 @@ MonoBehaviour:
|
||||
attitudeResponseSpeed: 1
|
||||
mainEngine: {fileID: 1437557825}
|
||||
gimbalSystem: {fileID: 1437557827}
|
||||
rcsController: {fileID: 28211676}
|
||||
throttleDisplay: {fileID: 348618513}
|
||||
--- !u!114 &986986473
|
||||
MonoBehaviour:
|
||||
@@ -1611,6 +1624,7 @@ MonoBehaviour:
|
||||
m_EditorClassIdentifier: '::'
|
||||
mainEngine: {fileID: 1437557825}
|
||||
gimbalSystem: {fileID: 1437557827}
|
||||
rcsController: {fileID: 28211676}
|
||||
rb: {fileID: 1172921199}
|
||||
altimeter: {fileID: 894314916}
|
||||
physicsTransitionAltitude: 1000
|
||||
@@ -1898,6 +1912,7 @@ MonoBehaviour:
|
||||
m_Name:
|
||||
m_EditorClassIdentifier: '::'
|
||||
fuelAmount: 1000
|
||||
rcsFuelAmount: 200
|
||||
--- !u!114 &1437557827
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
|
||||
@@ -3,19 +3,32 @@ using UnityEngine;
|
||||
public class FuelTank : MonoBehaviour
|
||||
{
|
||||
public float fuelAmount = 1000f; // kg of fuel available in the tank
|
||||
public float rcsFuelAmount = 200f; // kg of monopropellant available for RCS
|
||||
public bool isEmpty => fuelAmount <= 0f;
|
||||
public bool isRcsEmpty => rcsFuelAmount <= 0f;
|
||||
public float RequestFuel(float amount)
|
||||
{
|
||||
float fuelGiven = Mathf.Min(amount, fuelAmount);
|
||||
fuelAmount -= fuelGiven;
|
||||
return fuelGiven;
|
||||
}
|
||||
public float RequestRcsFuel(float amount)
|
||||
{
|
||||
float fuelGiven = Mathf.Min(amount, rcsFuelAmount);
|
||||
rcsFuelAmount -= fuelGiven;
|
||||
return fuelGiven;
|
||||
}
|
||||
public float GetFuelAmount() => fuelAmount;
|
||||
public float GetRcsFuelAmount() => rcsFuelAmount;
|
||||
void Update()
|
||||
{
|
||||
if (fuelAmount <= 0f)
|
||||
{
|
||||
fuelAmount = 0f;
|
||||
}
|
||||
if (rcsFuelAmount <= 0f)
|
||||
{
|
||||
rcsFuelAmount = 0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ public class InstrumentManager : MonoBehaviour
|
||||
[Header("References")]
|
||||
public MainEngine mainEngine; // Reference to your engine script
|
||||
public GimbalSystem gimbalSystem; // Reference to gimbal control system
|
||||
public RCSController rcsController; // Reference to RCS control system
|
||||
// Referance to TextMeshPro Throttle slider
|
||||
public Slider throttleDisplay; // Reference to UI Slider component
|
||||
|
||||
@@ -80,7 +81,7 @@ public class InstrumentManager : MonoBehaviour
|
||||
}
|
||||
|
||||
// Handle attitude control input
|
||||
if (attitudeAction != null && gimbalSystem != null)
|
||||
if (attitudeAction != null)
|
||||
{
|
||||
// Read Vector3 input
|
||||
Vector3 attitudeInput = attitudeAction.action.ReadValue<Vector3>();
|
||||
@@ -92,9 +93,20 @@ public class InstrumentManager : MonoBehaviour
|
||||
// Y (up/down) → Pitch
|
||||
// X (left/right) → Yaw
|
||||
// Z (forward/backward) → Roll
|
||||
gimbalSystem.SetPitchInput(attitudeInput.y);
|
||||
gimbalSystem.SetYawInput(attitudeInput.x);
|
||||
gimbalSystem.SetRollInput(attitudeInput.z);
|
||||
if (gimbalSystem != null)
|
||||
{
|
||||
gimbalSystem.SetPitchInput(attitudeInput.y);
|
||||
gimbalSystem.SetYawInput(attitudeInput.x);
|
||||
gimbalSystem.SetRollInput(attitudeInput.z);
|
||||
}
|
||||
|
||||
// Map inputs to RCS controller:
|
||||
// Vector3(x=yaw, y=pitch, z=roll)
|
||||
if (rcsController != null)
|
||||
{
|
||||
Vector3 rcsInput = new Vector3(attitudeInput.x, attitudeInput.y, attitudeInput.z);
|
||||
rcsController.SetControlInput(rcsInput);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,15 +2,92 @@ using UnityEngine;
|
||||
|
||||
public class RCSController : MonoBehaviour
|
||||
{
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
[Header("References")]
|
||||
public Rigidbody rb;
|
||||
public FuelTank fuelTank;
|
||||
|
||||
[Header("RCS Settings")]
|
||||
public bool rcsEnabled = true;
|
||||
public bool consumeFuel = true;
|
||||
public float maxTorque = 2000f; // N*m
|
||||
public float torqueResponseRate = 6000f; // N*m per second
|
||||
public float leverArm = 2f; // meters
|
||||
public float Isp = 250f; // seconds
|
||||
public float inputDeadzone = 0.05f;
|
||||
|
||||
[Header("Input")]
|
||||
public Vector3 controlInput; // x = yaw, y = pitch, z = roll (-1 to 1)
|
||||
|
||||
[Header("Output")]
|
||||
public Vector3 appliedTorque = Vector3.zero; // world space
|
||||
public float fuelFlowRate = 0f; // kg/s
|
||||
|
||||
private const float g0 = 9.80665f;
|
||||
private Vector3 currentLocalTorque = Vector3.zero;
|
||||
|
||||
void FixedUpdate()
|
||||
{
|
||||
|
||||
if (!rcsEnabled || rb == null)
|
||||
{
|
||||
appliedTorque = Vector3.zero;
|
||||
currentLocalTorque = Vector3.zero;
|
||||
fuelFlowRate = 0f;
|
||||
return;
|
||||
}
|
||||
|
||||
Vector3 clampedInput = new Vector3(
|
||||
Mathf.Abs(controlInput.x) < inputDeadzone ? 0f : Mathf.Clamp(controlInput.x, -1f, 1f),
|
||||
Mathf.Abs(controlInput.y) < inputDeadzone ? 0f : Mathf.Clamp(controlInput.y, -1f, 1f),
|
||||
Mathf.Abs(controlInput.z) < inputDeadzone ? 0f : Mathf.Clamp(controlInput.z, -1f, 1f)
|
||||
);
|
||||
|
||||
float pitchInput = clampedInput.y;
|
||||
float yawInput = clampedInput.x;
|
||||
float rollInput = clampedInput.z;
|
||||
|
||||
Vector3 targetLocalTorque = new Vector3(pitchInput, yawInput, rollInput) * maxTorque;
|
||||
float maxDelta = torqueResponseRate * Time.fixedDeltaTime;
|
||||
currentLocalTorque = Vector3.MoveTowards(currentLocalTorque, targetLocalTorque, maxDelta);
|
||||
|
||||
Vector3 fueledLocalTorque = ApplyFuelLimit(currentLocalTorque);
|
||||
appliedTorque = rb.rotation * fueledLocalTorque;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
private Vector3 ApplyFuelLimit(Vector3 desiredLocalTorque)
|
||||
{
|
||||
|
||||
fuelFlowRate = 0f;
|
||||
if (!consumeFuel || fuelTank == null)
|
||||
{
|
||||
return desiredLocalTorque;
|
||||
}
|
||||
|
||||
if (leverArm <= 0f || Isp <= 0f)
|
||||
{
|
||||
return desiredLocalTorque;
|
||||
}
|
||||
|
||||
float torqueMagnitude = desiredLocalTorque.magnitude;
|
||||
if (torqueMagnitude <= 0f)
|
||||
{
|
||||
return Vector3.zero;
|
||||
}
|
||||
|
||||
float requiredForce = torqueMagnitude / leverArm;
|
||||
fuelFlowRate = requiredForce / (Isp * g0);
|
||||
float fuelRequested = fuelFlowRate * Time.fixedDeltaTime;
|
||||
float fuelProvided = fuelTank.RequestRcsFuel(fuelRequested);
|
||||
|
||||
if (fuelProvided < fuelRequested && fuelRequested > 0f)
|
||||
{
|
||||
float scale = Mathf.Clamp01(fuelProvided / fuelRequested);
|
||||
return desiredLocalTorque * scale;
|
||||
}
|
||||
|
||||
return desiredLocalTorque;
|
||||
}
|
||||
|
||||
public void SetControlInput(Vector3 input)
|
||||
{
|
||||
controlInput = input;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ public class RocketPhysics : MonoBehaviour
|
||||
[Header("References")]
|
||||
public MainEngine mainEngine;
|
||||
public GimbalSystem gimbalSystem;
|
||||
public RCSController rcsController;
|
||||
public Rigidbody rb;
|
||||
public Altimeter altimeter;
|
||||
|
||||
@@ -102,10 +103,18 @@ public class RocketPhysics : MonoBehaviour
|
||||
rb.AddForce(gravityForce, ForceMode.Force);
|
||||
rb.AddForce(thrustForce, ForceMode.Force);
|
||||
|
||||
// Apply gimbal torque if available
|
||||
Vector3 totalTorque = Vector3.zero;
|
||||
if (gimbalSystem != null)
|
||||
{
|
||||
rb.AddTorque(gimbalSystem.appliedTorque, ForceMode.Force);
|
||||
totalTorque += gimbalSystem.appliedTorque;
|
||||
}
|
||||
if (rcsController != null)
|
||||
{
|
||||
totalTorque += rcsController.appliedTorque;
|
||||
}
|
||||
if (totalTorque.sqrMagnitude > 0f)
|
||||
{
|
||||
rb.AddTorque(totalTorque, ForceMode.Force);
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -149,10 +158,18 @@ public class RocketPhysics : MonoBehaviour
|
||||
// Only apply to rigidbody if not frozen
|
||||
if (!isFrozen)
|
||||
{
|
||||
// Apply gimbal torque if available
|
||||
if (gimbalSystem != null && gimbalSystem.appliedTorque.magnitude > 0f)
|
||||
Vector3 totalTorque = Vector3.zero;
|
||||
if (gimbalSystem != null)
|
||||
{
|
||||
ApplyManualTorque(gimbalSystem.appliedTorque);
|
||||
totalTorque += gimbalSystem.appliedTorque;
|
||||
}
|
||||
if (rcsController != null)
|
||||
{
|
||||
totalTorque += rcsController.appliedTorque;
|
||||
}
|
||||
if (totalTorque.sqrMagnitude > 0f)
|
||||
{
|
||||
ApplyManualTorque(totalTorque);
|
||||
}
|
||||
|
||||
// Update Unity transform (kinematic mode)
|
||||
|
||||
Reference in New Issue
Block a user