Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- //public enum Player_State
- //{
- // Accelerating,
- // Deccelerating,
- // Stop,
- //};
- public class Jeepney_Move_P : MonoBehaviour {
- public GameObject[] Trigger_Colliders = new GameObject[2];
- //int Collider_ID;
- //public int Trigger_ID;
- public WheelCollider []WheelColliders = new WheelCollider[4];
- public GameObject []WheelMeshes = new GameObject[4];
- public float Turn_Angle;
- public float Turn_Speed;
- public float Max_TAngle;
- public Vector3 Center_Of_Mass;
- int wheel_Id;
- float shiftDelay = 0.0f;
- //
- public float torque; // the base power of the engine (per wheel, and before gears) suggested value:100
- public float brakeTorque; // the power of the braks (per wheel) suggested value 2000
- public float shiftDownRPM; // rpm script will shift gear down suggested value 1500
- public float shiftUpRPM; // rpm script will shift gear up suggested value 2500
- public float idleRPM; // idle rpm suggested value 500
- //
- // gear ratios (index 0 is reverse)
- public float[] gears = { -10f, 9f, 6f, 4.5f, 3f, 2.5f };
- // automatic, if true car shifts automatically up/down
- public bool automatic = true;
- // table of efficiency at certain RPM, in tableStep RPM increases, 1.0f is 100% efficient
- // at the given RPM, current table has 100% at around 2000RPM
- 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 };
- // the scale of the indices in table, so with 250f, 750RPM translates to efficiencyTable[3].
- float efficiencyTableStep = 250.0f;
- public int currentGear = 1; // duh.
- //
- Transform ObjTransform;
- Rigidbody Physics_Object;
- //0,1 Front wheels
- //2,3 Rear wheels
- // Use this for initialization
- void Start () {
- Physics_Object = this.gameObject.GetComponent<Rigidbody>();
- Physics_Object.centerOfMass = Center_Of_Mass;
- }
- // Update is called once per frame
- void Update () {
- }
- void FixedUpdate()
- {
- //Gear_Change();
- //Obj_AutoShifting();
- Steering();
- Acceleration();
- Engine_Physics();
- Rotate_WheelMesh ();
- GetTouchPositions ();
- Attached_Colliders ();
- }
- public void Gear_Change()
- {
- ShiftUp();
- ShiftDown();
- }
- public void ShiftDown()
- {
- if(TouchDeltaPosition.y < 0.0f)
- {
- float now = Time.timeSinceLevelLoad;
- if(now < shiftDelay) return;
- if(currentGear > 0)
- {
- currentGear --;
- }
- shiftDelay = now + 0.1f;
- }
- }
- public void ShiftUp()
- {
- if(TouchDeltaPosition.y > 0.0f)
- {
- float now = Time.timeSinceLevelLoad;
- if(now < shiftDelay) return;
- //
- if(currentGear < gears.Length - 1)
- {
- currentGear ++;
- shiftDelay = now + 1.0f;
- }
- }
- }
- float rpm = 0.0f;
- int motorizedWheels = 2;
- float wantedRPM = 0.0f; // rpm the engine tries to reach
- float motorRPM = 0.0f;
- public float accel = 0; // accelerating -1.0 .. 1.0
- public bool brake = false; // braking (true is brake)
- //
- public float touchSpd = 0.1f;
- Vector2 TouchDeltaPosition = Vector2.zero;
- void GetTouchPositions()
- {
- if((Input.touchCount > 0) && (Input.GetTouch(0).phase == TouchPhase.Moved))
- {
- TouchDeltaPosition = Input.GetTouch(0).deltaPosition;
- }
- if (TouchDeltaPosition.y < 0.0f)
- {
- accel -= 1.0f;
- if (accel < 0.0f) {
- // if we try to decelerate we brake.
- brake = true;
- accel = 0.0f;
- wantedRPM = 0.0f;
- }
- // float now = Time.timeSinceLevelLoad;
- // if(now < shiftDelay) return;
- // //
- // if(currentGear < gears.Length - 1)
- // {
- // currentGear ++;
- // shiftDelay = now + 1.0f;
- // }
- //print(TouchDeltaPosition.y);
- }
- else if(TouchDeltaPosition.y > 0.0f)
- {
- if(currentGear < 5)
- {
- brake = false;
- }
- if(accel < 1.0f)
- accel = 1.0f;
- }
- }
- void Obj_AutoShifting()
- {
- // handle automatic shifting
- if (automatic && (currentGear == 1) && (accel < 0.0f)) {
- ShiftDown(); // reverse
- }
- else if (automatic && (currentGear == 0) && (accel > 0.0f)) {
- ShiftUp(); // go from reverse to first gear
- }
- else if (automatic && (motorRPM > shiftUpRPM) && (accel > 0.0f)) {
- ShiftUp(); // shift up
- }
- else if (automatic && (motorRPM < shiftDownRPM) && (currentGear > 1)) {
- ShiftDown(); // shift down
- }
- if (automatic && (currentGear == 0)) {
- accel = - accel; // in automatic mode we need to hold arrow down for reverse
- }
- if (accel < 0.0f) {
- // if we try to decelerate we brake.
- brake = true;
- accel = 0.0f;
- wantedRPM = 0.0f;
- }
- }
- void Rotate_WheelMesh()
- {
- }
- void Acceleration()
- {
- if(Input.GetKey(KeyCode.W) && accel < 1.0f)
- {
- accel += 1.0f;
- brake = false;
- }
- else if(Input.GetKey(KeyCode.S) && accel > -1.0f)
- {
- accel -= 1.0f;
- if(brake != true)
- {
- brake = true;
- }
- else
- brake = false;
- }
- if(Input.GetKey(KeyCode.Space))
- {
- if(brake != true)
- {
- brake = true;
- }
- else
- brake = false;
- }
- }
- void Steering()
- {
- WheelColliders[0].steerAngle = Turn_Angle;
- WheelColliders[1].steerAngle = Turn_Angle;
- }
- void Engine_Physics()
- {
- wantedRPM = (5500.0f * accel) * 0.1f + wantedRPM * 0.9f;
- // calculate the local rotation of the wheels from the delta time and rpm
- // then set the local rotation accordingly (also adjust for steering)
- //w.rotation = Mathf.Repeat(w.rotation + delta * col.rpm * 360.0f / 60.0f, 360.0f);
- if(motorizedWheels > 1)
- {
- rpm = rpm / motorizedWheels;
- }
- motorRPM = 0.95f * motorRPM + 0.05f * Mathf.Abs(rpm * gears[currentGear]);
- if(motorRPM > 5500.0f) motorRPM = 5500.0f;
- //calculate efficiency of Engine
- int index = (int)(motorRPM / efficiencyTableStep);
- if (index >= efficiencyTable.Length) index = efficiencyTable.Length - 1;
- if (index < 0) index = 0;
- // calculate torque using gears and efficiency table
- float newTorque = torque * gears[currentGear] * efficiencyTable[index];
- //WheelCollider W_colL = WheelColliders[2]; //left rear wheel;
- //WheelCollider W_colR = WheelColliders[3]; //right rear wheel;
- if(Mathf.Abs(WheelColliders[2].rpm) > Mathf.Abs(wantedRPM))
- {
- //W_colL.motorTorque = 0;
- //W_colR.motorTorque = 0;
- WheelColliders[2].motorTorque = 0;
- WheelColliders[3].motorTorque = 0;
- WheelColliders[1].motorTorque = 0;
- WheelColliders[0].motorTorque = 0;
- }
- else
- {
- float curTorqueL = WheelColliders[2].motorTorque;
- float curTorqueR = WheelColliders[3].motorTorque;
- WheelColliders[2].motorTorque = curTorqueL * 0.9f + newTorque * 0.1f;
- WheelColliders[3].motorTorque = curTorqueR * 0.9f + newTorque * 0.1f;
- }
- WheelColliders[0].brakeTorque = (brake)?brakeTorque:0.0f;
- WheelColliders[1].brakeTorque = (brake)?brakeTorque:0.0f;
- WheelColliders[2].brakeTorque = (brake)?brakeTorque:0.0f;
- WheelColliders[3].brakeTorque = (brake)?brakeTorque:0.0f;
- }
- public void OnGUI() {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement