Goose30

engine script

Apr 4th, 2026
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3.  
  4. [Serializable]
  5. public class Engine : MonoBehaviour
  6. {
  7. //defaults based on 2024 5.0L V8 Ford Coyote engine
  8. public float idleRPM = 700f; // RPM at idle
  9. public float maxRPM = 7500f; // RPM at redline
  10. public float[] gearRatios = { 4.696f, 2.985f, 2.146f, 1.769f, 1.520f, 1.275f, 1f, 0.854f, 0.689f, 0.636f }; // 10 speed automatic
  11. public float finalDriveRatio = 3.15f;
  12. public float transmissionEfficiency = 0.85f; // percentage of engine torque that actually reaches the wheels
  13. public AnimationCurve torqueCurve;
  14. public bool automatic = true;
  15. public int gear {private set; get;} = 0; // 0 is 1st
  16. public float rpm {private set; get;} = 0f;
  17.  
  18. private int prevGear = -1; // avoid quick gear drop
  19. private float lastShiftTime = 0f;
  20. private float shiftCooldown = 1f;
  21. private bool shifting = false;
  22. private float gearShiftTime = 0.15f; // time it takes transmission to shift gears, mustang is a bit slow
  23.  
  24. private bool engineOn = false;
  25.  
  26. public void StartEngine()
  27. {
  28. engineOn = true;
  29. rpm = idleRPM;
  30. }
  31.  
  32. public void SetRPM(float newRPM)
  33. {
  34. if (!engineOn) return;
  35.  
  36. rpm = Mathf.Clamp(newRPM, idleRPM, maxRPM);
  37. }
  38.  
  39. public float GetTorque(float wheelRadius, float throttle)
  40. {
  41. // always at max torque for now. Change to be maxTorque(rpm) * throttle input later
  42. float maxTorqueAtRPM = torqueCurve.Evaluate(rpm);
  43. float totalGearRatio = gearRatios[gear] * finalDriveRatio;
  44. float wheelTorque = maxTorqueAtRPM * totalGearRatio * throttle;
  45. return wheelTorque * transmissionEfficiency / wheelRadius;
  46. }
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment