Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- [Serializable]
- public class Engine : MonoBehaviour
- {
- //defaults based on 2024 5.0L V8 Ford Coyote engine
- public float idleRPM = 700f; // RPM at idle
- public float maxRPM = 7500f; // RPM at redline
- 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
- public float finalDriveRatio = 3.15f;
- public float transmissionEfficiency = 0.85f; // percentage of engine torque that actually reaches the wheels
- public AnimationCurve torqueCurve;
- public bool automatic = true;
- public int gear {private set; get;} = 0; // 0 is 1st
- public float rpm {private set; get;} = 0f;
- private int prevGear = -1; // avoid quick gear drop
- private float lastShiftTime = 0f;
- private float shiftCooldown = 1f;
- private bool shifting = false;
- private float gearShiftTime = 0.15f; // time it takes transmission to shift gears, mustang is a bit slow
- private bool engineOn = false;
- public void StartEngine()
- {
- engineOn = true;
- rpm = idleRPM;
- }
- public void SetRPM(float newRPM)
- {
- if (!engineOn) return;
- rpm = Mathf.Clamp(newRPM, idleRPM, maxRPM);
- }
- public float GetTorque(float wheelRadius, float throttle)
- {
- // always at max torque for now. Change to be maxTorque(rpm) * throttle input later
- float maxTorqueAtRPM = torqueCurve.Evaluate(rpm);
- float totalGearRatio = gearRatios[gear] * finalDriveRatio;
- float wheelTorque = maxTorqueAtRPM * totalGearRatio * throttle;
- return wheelTorque * transmissionEfficiency / wheelRadius;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment