Advertisement
Placido_GDD

Car_Move

May 31st, 2015
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.88 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. //public enum Player_State
  5. //{
  6. //  Accelerating,
  7. //  Deccelerating,
  8. //  Stop,
  9. //};
  10.  
  11. public class Jeepney_Move_P : MonoBehaviour {
  12.  
  13.     public GameObject[] Trigger_Colliders = new GameObject[2];
  14.     //int Collider_ID;
  15.     //public int Trigger_ID;
  16.     public WheelCollider []WheelColliders = new WheelCollider[4];
  17.     public GameObject    []WheelMeshes = new GameObject[4];
  18.     public float Turn_Angle;
  19.     public float Turn_Speed;
  20.     public float Max_TAngle;
  21.     public Vector3 Center_Of_Mass;
  22.     int wheel_Id;
  23.     float shiftDelay = 0.0f;
  24.     //
  25.     public float torque; // the base power of the engine (per wheel, and before gears) suggested value:100
  26.     public float brakeTorque; // the power of the braks (per wheel) suggested value 2000
  27.     public float shiftDownRPM; // rpm script will shift gear down suggested value 1500
  28.     public float shiftUpRPM; // rpm script will shift gear up suggested value 2500
  29.     public float idleRPM; // idle rpm suggested value 500
  30.     //
  31.     // gear ratios (index 0 is reverse)
  32.     public float[] gears = { -10f, 9f, 6f, 4.5f, 3f, 2.5f };
  33.     // automatic, if true car shifts automatically up/down
  34.     public bool automatic = true;
  35.     // table of efficiency at certain RPM, in tableStep RPM increases, 1.0f is 100% efficient
  36.     // at the given RPM, current table has 100% at around 2000RPM
  37.     float[] efficiencyTable = { 0.6f, 0.65f, 0.7f, 0.75f, 0.8f, 0.85f, 0.9f, 1.0f, 1.0f, 0.95f, 0.80f, 0.70f, 0.60f, 0.5f, 0.45f, 0.40f, 0.36f, 0.33f, 0.30f, 0.20f, 0.10f, 0.05f };   
  38.     // the scale of the indices in table, so with 250f, 750RPM translates to efficiencyTable[3].
  39.     float efficiencyTableStep = 250.0f;
  40.    
  41.     public int currentGear = 1; // duh.
  42.  
  43.     //
  44.     Transform ObjTransform;
  45.     Rigidbody Physics_Object;
  46.  
  47.  
  48.     //0,1 Front wheels
  49.     //2,3 Rear wheels
  50.     // Use this for initialization
  51.     void Start () {
  52.         Physics_Object = this.gameObject.GetComponent<Rigidbody>();
  53.         Physics_Object.centerOfMass = Center_Of_Mass;
  54.     }
  55.    
  56.     // Update is called once per frame
  57.     void Update () {
  58.  
  59.  
  60.     }
  61.    
  62.     void FixedUpdate()
  63.     {
  64.         //Gear_Change();
  65.         //Obj_AutoShifting();
  66.         Steering();
  67.         Acceleration();
  68.         Engine_Physics();
  69.         Rotate_WheelMesh ();
  70.         GetTouchPositions ();
  71.         Attached_Colliders ();
  72.     }
  73.     public void Gear_Change()
  74.     {
  75.         ShiftUp();
  76.         ShiftDown();
  77.     }
  78.     public void ShiftDown()
  79.     {
  80.         if(TouchDeltaPosition.y < 0.0f)
  81.         {
  82.             float now = Time.timeSinceLevelLoad;
  83.             if(now < shiftDelay) return;
  84.             if(currentGear > 0)
  85.             {
  86.                 currentGear --;
  87.             }
  88.             shiftDelay = now + 0.1f;
  89.         }
  90.     }
  91.     public void ShiftUp()
  92.     {
  93.         if(TouchDeltaPosition.y > 0.0f)
  94.         {
  95.             float now = Time.timeSinceLevelLoad;
  96.             if(now < shiftDelay) return;
  97.             //
  98.             if(currentGear < gears.Length - 1)
  99.             {
  100.                 currentGear ++;
  101.                 shiftDelay = now + 1.0f;
  102.             }
  103.         }
  104.     }
  105.     float rpm = 0.0f;
  106.     int motorizedWheels = 2;
  107.     float wantedRPM = 0.0f; // rpm the engine tries to reach
  108.     float motorRPM = 0.0f;
  109.     public float accel = 0; // accelerating -1.0 .. 1.0
  110.     public bool brake = false; // braking (true is brake)
  111.     //
  112.  
  113.     public float touchSpd = 0.1f;
  114.     Vector2 TouchDeltaPosition = Vector2.zero;
  115.     void GetTouchPositions()
  116.     {  
  117.         if((Input.touchCount > 0) && (Input.GetTouch(0).phase == TouchPhase.Moved))
  118.         {
  119.             TouchDeltaPosition = Input.GetTouch(0).deltaPosition;
  120.         }
  121.         if (TouchDeltaPosition.y < 0.0f)
  122.         {
  123.             accel -= 1.0f;
  124.             if (accel < 0.0f) {
  125.                 // if we try to decelerate we brake.
  126.                 brake = true;
  127.                 accel = 0.0f;
  128.                 wantedRPM = 0.0f;
  129.             }
  130. //          float now = Time.timeSinceLevelLoad;
  131. //          if(now < shiftDelay) return;
  132. //          //
  133. //          if(currentGear < gears.Length - 1)
  134. //          {
  135. //              currentGear ++;
  136. //              shiftDelay = now + 1.0f;
  137. //          }
  138.             //print(TouchDeltaPosition.y);
  139.         }
  140.         else if(TouchDeltaPosition.y > 0.0f)
  141.         {
  142.             if(currentGear < 5)
  143.             {
  144.                 brake = false;
  145.             }
  146.             if(accel < 1.0f)
  147.                 accel = 1.0f;
  148.         }
  149.     }
  150.     void Obj_AutoShifting()
  151.     {
  152.         // handle automatic shifting
  153.         if (automatic && (currentGear == 1) && (accel < 0.0f)) {
  154.             ShiftDown(); // reverse
  155.         }
  156.         else if (automatic && (currentGear == 0) && (accel > 0.0f)) {
  157.             ShiftUp(); // go from reverse to first gear
  158.         }
  159.         else if (automatic && (motorRPM > shiftUpRPM) && (accel > 0.0f)) {
  160.             ShiftUp(); // shift up
  161.         }
  162.         else if (automatic && (motorRPM < shiftDownRPM) && (currentGear > 1)) {
  163.             ShiftDown(); // shift down
  164.         }
  165.         if (automatic && (currentGear == 0)) {
  166.             accel = - accel; // in automatic mode we need to hold arrow down for reverse
  167.         }
  168.         if (accel < 0.0f) {
  169.             // if we try to decelerate we brake.
  170.             brake = true;
  171.             accel = 0.0f;
  172.             wantedRPM = 0.0f;
  173.         }
  174.  
  175.     }
  176.     void Rotate_WheelMesh()
  177.     {
  178.     }
  179.     void Acceleration()
  180.     {
  181.         if(Input.GetKey(KeyCode.W) && accel < 1.0f)
  182.         {
  183.             accel += 1.0f;
  184.             brake = false;
  185.         }
  186.         else if(Input.GetKey(KeyCode.S) && accel > -1.0f)
  187.         {
  188.             accel -= 1.0f;
  189.             if(brake != true)
  190.             {
  191.                 brake = true;
  192.             }
  193.             else
  194.                 brake = false;
  195.         }
  196.  
  197.         if(Input.GetKey(KeyCode.Space))
  198.         {
  199.             if(brake != true)
  200.             {
  201.                 brake = true;
  202.             }
  203.             else
  204.                 brake = false;
  205.         }
  206.  
  207.     }
  208.  
  209.  
  210.     void Steering()
  211.     {
  212.         WheelColliders[0].steerAngle = Turn_Angle;
  213.         WheelColliders[1].steerAngle = Turn_Angle;
  214.     }
  215.     void Engine_Physics()
  216.     {
  217.         wantedRPM = (5500.0f * accel) * 0.1f + wantedRPM * 0.9f;
  218.         // calculate the local rotation of the wheels from the delta time and rpm
  219.         // then set the local rotation accordingly (also adjust for steering)
  220.         //w.rotation = Mathf.Repeat(w.rotation + delta * col.rpm * 360.0f / 60.0f, 360.0f);
  221.         if(motorizedWheels > 1)
  222.         {
  223.             rpm = rpm / motorizedWheels;
  224.         }
  225.         motorRPM = 0.95f * motorRPM + 0.05f * Mathf.Abs(rpm * gears[currentGear]);
  226.         if(motorRPM > 5500.0f) motorRPM = 5500.0f;
  227.         //calculate efficiency of Engine
  228.         int index = (int)(motorRPM / efficiencyTableStep);
  229.         if (index >= efficiencyTable.Length) index = efficiencyTable.Length - 1;
  230.         if (index < 0) index = 0;
  231.         // calculate torque using gears and efficiency table
  232.         float newTorque = torque * gears[currentGear] * efficiencyTable[index];
  233.         //WheelCollider W_colL = WheelColliders[2]; //left rear wheel;
  234.         //WheelCollider W_colR = WheelColliders[3]; //right rear wheel;
  235.         if(Mathf.Abs(WheelColliders[2].rpm) > Mathf.Abs(wantedRPM))
  236.         {
  237.             //W_colL.motorTorque = 0;
  238.             //W_colR.motorTorque = 0;
  239.             WheelColliders[2].motorTorque = 0;
  240.             WheelColliders[3].motorTorque = 0;
  241.             WheelColliders[1].motorTorque = 0;
  242.             WheelColliders[0].motorTorque = 0;
  243.         }
  244.         else
  245.         {
  246.             float curTorqueL = WheelColliders[2].motorTorque;
  247.             float curTorqueR = WheelColliders[3].motorTorque;
  248.             WheelColliders[2].motorTorque = curTorqueL * 0.9f + newTorque * 0.1f;
  249.             WheelColliders[3].motorTorque = curTorqueR * 0.9f + newTorque * 0.1f;
  250.         }
  251.         WheelColliders[0].brakeTorque = (brake)?brakeTorque:0.0f;
  252.         WheelColliders[1].brakeTorque = (brake)?brakeTorque:0.0f;
  253.         WheelColliders[2].brakeTorque = (brake)?brakeTorque:0.0f;
  254.         WheelColliders[3].brakeTorque = (brake)?brakeTorque:0.0f;
  255.     }
  256.  
  257.     public void OnGUI() {
  258.     }
  259.  
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement