Advertisement
Guest User

Untitled

a guest
Jul 30th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.45 KB | None | 0 0
  1. /*
  2.  
  3.     - Scale torque curve down to 0.1 - 1.0 and use MaxRPM and MaxTorque
  4.     - Use curve for wheel damping rate. Rate decrease with speed
  5.     [NEW] - Add more damping or some brakeTorque if clutch engaged and not accelerating
  6.     [NEW] - Add wheel damping or braketorque when engineRPM more than MaxRPM
  7.     - Make it so if they skip 2 gears, they acceleration will be slow or none at all. Possibly just increase the RPM drop when changing gears so that the torque produce won't be able to keep the car at current speed
  8.     [NEW] - When clutch is engaged only wheel rpm will affect engine RPM(i.e modify calculation formula)
  9.     [NEW] - Equalise wheel and engine RPM when clutch engaged. If wheelRPM(Make sure multiplied with GearRatio and FinalDrive) more than engineRPM and (if low gear[ oO ] then (engineRPM increase is less and wheelRPM decrease more) or if high gear [ Oo ] then (engineRPM increase more and wheelRPM decrease less)) or if wheelRPM(Make sure multiplied with GearRatio and FinalDrive) less than engineRPM then (FOR THIS, ONLY WORKS WHEN ENGINE ISN'T ACCELERATING CAUSE WE HAVE SEPARATE CALCULATIONS FOR WHEN ENGINE IS ACCELERATING <<<< Revise to use EngineResistance and combine both calculations)(if low gear[ oO ] then (engineRPM decrease is less and wheelRPM increase more) or if high gear [ Oo ] then (engineRPM decrease more and wheelRPM increase less)). This physics is due to the gear ratio of engine and wheel and which side(Wheel or engine) is providing more torque than the other
  10.     [NEW] - Engine die off when stall
  11.     - For some of ^ to be done, EngineRPM and wheel RPM might need to be 2 separate systems
  12.     [NEW] - Possibly just add a new parameter named EngineResistance and use that for clutch and stuffs to reduce engine torque
  13.     [NEW] - Add car temperature system and when engine RPM more than MaxRPM or (too high but less than MaxRPM) for too long then temperature increase. When temperature too hot, maybe the car dies off or car engine breaks down or car health(For survival game) decrease
  14.     -
  15.    
  16.     LOGIC
  17.         - Engine accelerating
  18.             - Low gear
  19.                 -
  20.             - High gear
  21.                 -
  22.         - Engine not accelerating
  23.             - Low gear
  24.                 -
  25.             - High gear
  26.                 -
  27.  
  28. */
  29. using UnityEngine;
  30. using UnityEngine.UI;
  31. using System.Collections;
  32.  
  33. public class CarSystem:MonoBehaviour
  34. {
  35.     public enum Transmissions {Manual, Auto, Semi};
  36.     public Transmissions Transmission = Transmissions.Semi;
  37.     public enum DriveWheels {All, Front, Rear};
  38.     public DriveWheels DriveWheel = DriveWheels.Rear;
  39.     public enum SteerWheels {All, Front, Rear};
  40.     public SteerWheels SteerWheel = SteerWheels.Front;
  41.    
  42.     public AnimationCurve TorqueCurve;
  43.     public float[] GearRatio;
  44.     public float FinalDriveRatio;
  45.    
  46.     public WheelCollider WheelColFR;
  47.     public WheelCollider WheelColFL;
  48.     public WheelCollider WheelColRR;
  49.     public WheelCollider WheelColRL;
  50.    
  51.     public float VSSVelocity = 50;
  52.     public int VSSBelow = 150;
  53.     public int VSSAbove = 250;
  54.    
  55.     public float TorqueMultiplier = 3;
  56.     public float MinRPM = 1250;
  57.     public float MaxRPM = 9000;
  58.     public float MaxSteer = 30;
  59.     public float HandBrakeTorque = 5000;
  60.     public float BrakeTorque = 1250;
  61.     public float AntiRoll = 2000;
  62.    
  63.     public GameObject Meter;
  64.     public Text GearText;
  65.    
  66.     float EngineRPM = 0;
  67.     int GearIndex = 0;
  68.    
  69.     MeterManager MeterUI;
  70.     Rigidbody rb;
  71.    
  72.     void Start()
  73.     {
  74.         MeterUI = Meter.transform.GetComponent<MeterManager>();
  75.         rb = GetComponent<Rigidbody>();
  76.        
  77.         WheelColFR.ConfigureVehicleSubsteps(VSSVelocity, VSSBelow, VSSAbove);
  78.     }
  79.    
  80.     void FixedUpdate()
  81.     {
  82.         EngineRPM = MinRPM + (((WheelColRR.rpm + WheelColRL.rpm) / 2) * FinalDriveRatio * GearRatio[GearIndex]);
  83.         EngineRPM = Mathf.Clamp(EngineRPM, 0, MaxRPM);
  84.        
  85.         float TotalTorque = TorqueCurve.Evaluate(EngineRPM) * TorqueMultiplier;
  86.        
  87.         if(Input.GetKey(KeyCode.C))
  88.         {
  89.             WheelColRR.motorTorque = 0;
  90.             WheelColRL.motorTorque = 0;
  91.  
  92.         }
  93.         else
  94.         {
  95.             WheelColRR.motorTorque = Input.GetAxis("Vertical") * TotalTorque;
  96.             WheelColRL.motorTorque = Input.GetAxis("Vertical") * TotalTorque;
  97.         }
  98.        
  99.         Debug.Log((TotalTorque).ToString("F4") + " | " + (WheelColRR.rpm).ToString("F4") + " | " + (WheelColFR.rpm).ToString("F4"));
  100.        
  101.         if(Input.GetButtonDown("ShiftUp") && GearIndex < GearRatio.Length - 1)
  102.         {
  103.             GearIndex++;
  104.         }
  105.        
  106.         if(Input.GetButtonDown("ShiftDown") && GearIndex > 0)
  107.         {
  108.             GearIndex--;
  109.         }
  110.        
  111.         WheelColFR.steerAngle = Input.GetAxis("Horizontal") * MaxSteer;
  112.         WheelColFL.steerAngle = Input.GetAxis("Horizontal") * MaxSteer;
  113.        
  114.         if(Input.GetKey(KeyCode.Space))
  115.         {
  116.             WheelColFR.brakeTorque = BrakeTorque;
  117.             WheelColFL.brakeTorque = BrakeTorque;
  118.             WheelColRR.brakeTorque = BrakeTorque;
  119.             WheelColRL.brakeTorque = BrakeTorque;
  120.         }
  121.         else
  122.         {
  123.             WheelColFR.brakeTorque = 0;
  124.             WheelColFL.brakeTorque = 0;
  125.             WheelColRR.brakeTorque = 0;
  126.             WheelColRL.brakeTorque = 0;
  127.         }
  128.        
  129.         //Stabilizer
  130.         for(int l = 0; l <= 3; l++)
  131.         {
  132.             WheelCollider Wheel1 = WheelColFR;
  133.             WheelCollider Wheel2 = WheelColFL;
  134.            
  135.             if(l == 0)
  136.             {
  137.                 Wheel1 = WheelColFR;
  138.                 Wheel2 = WheelColFL;
  139.             }
  140.             else if(l == 1)
  141.             {
  142.                 Wheel1 = WheelColRR;
  143.                 Wheel2 = WheelColRL;
  144.             }
  145.             else if(l == 2)
  146.             {
  147.                 Wheel1 = WheelColFR;
  148.                 Wheel2 = WheelColRR;
  149.             }
  150.             else if(l == 3)
  151.             {
  152.                 Wheel1 = WheelColFL;
  153.                 Wheel2 = WheelColRL;
  154.             }
  155.            
  156.             WheelHit hit;
  157.             float travel1 = 1;
  158.             float travel2 = 1;
  159.  
  160.             bool grounded1 = Wheel1.GetGroundHit(out hit);
  161.             if (grounded1) travel1 = (-Wheel1.transform.InverseTransformPoint(hit.point).y - Wheel1.radius) / Wheel1.suspensionDistance;
  162.  
  163.             bool grounded2 = Wheel2.GetGroundHit(out hit);
  164.             if (grounded2) travel2 = (-Wheel2.transform.InverseTransformPoint(hit.point).y - Wheel2.radius) / Wheel2.suspensionDistance;
  165.  
  166.             float antiRollForce = (travel1 - travel2) * AntiRoll;
  167.  
  168.             if (grounded1) GetComponent<Rigidbody>().AddForceAtPosition(Wheel1.transform.up * -antiRollForce, Wheel1.transform.position);
  169.             if (grounded2) GetComponent<Rigidbody>().AddForceAtPosition(Wheel2.transform.up * antiRollForce, Wheel2.transform.position);
  170.  
  171.             if(!grounded1 || !grounded2)
  172.             {
  173.                 Debug.Log(Wheel1.gameObject.name + " | " + Wheel2.gameObject.name);
  174.             }
  175.         }
  176.        
  177.         MeterUI.RPM.Amount = (EngineRPM / 1000) / MeterUI.RPM.MaxValue;
  178.         MeterUI.Speedo.Amount = (rb.velocity.magnitude * 3.6f) / MeterUI.Speedo.MaxValue;
  179.        
  180.         MeterUI.RPM.Display.text = (EngineRPM / 1000).ToString("F2") + " RPM";
  181.         MeterUI.Speedo.Display.text = (rb.velocity.magnitude * 3.6f).ToString("F0") + " KMH";
  182.         GearText.text = (GearIndex + 1).ToString();
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement