Advertisement
Guest User

car game

a guest
Feb 20th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.98 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityStandardAssets.CrossPlatformInput;
  6.  
  7. public class MovingCar : MonoBehaviour
  8. {
  9.     public Text TxtSpeed;
  10.     public WheelCollider front_left;
  11.     public WheelCollider front_right;
  12.     public WheelCollider back_left;
  13.     public WheelCollider back_right;
  14.     public Transform FL;
  15.     public Transform FR;
  16.     public Transform BL;
  17.     public Transform BR;
  18.     public float Torque;
  19.     public float speed;
  20.     public float MaxSpeed = 200f;
  21.     public int Brake = 10000;
  22.     public float coefAcceleration = 10f;
  23.     public float WheelAngleMax = 10f;
  24.     public float DAmax = 40f;
  25.     public bool freinge = false;
  26.     public GameObject BackLight;
  27.     private void Start()
  28.     {
  29.         GetComponent<Rigidbody>().centerOfMass = new Vector3(0f, -0.9f, 0.2f);
  30.     }
  31.     void Update()
  32.     {
  33.         float Val_path = speed / MaxSpeed + 1.5f;
  34.         speed = GetComponent<Rigidbody>().velocity.magnitude * 3.6f;
  35.         TxtSpeed.text = "Speed " + (int)speed;
  36.  
  37.         //Acceleration
  38.         if (CrossPlatformInputManager.GetAxis("Horizontal") > 0 && (speed < MaxSpeed))
  39.         {
  40.             if (!freinge)
  41.             {
  42.                 front_right.brakeTorque = 0;
  43.                 front_left.brakeTorque = 0;
  44.                 back_left.brakeTorque = 0;
  45.                 back_right.brakeTorque = 0;
  46.                 back_left.motorTorque = CrossPlatformInputManager.GetAxis("Vertical") * Torque * coefAcceleration * Time.deltaTime;
  47.                 back_right.motorTorque = CrossPlatformInputManager.GetAxis("Vertical") * Torque * coefAcceleration * Time.deltaTime;
  48.             }
  49.         }
  50.         if (CrossPlatformInputManager.GetAxis("Horizontal") == 0 && !freinge || speed > MaxSpeed)
  51.         {
  52.             if (GetComponent<Rigidbody>().velocity.y > 0)
  53.             {
  54.                 back_left.motorTorque = -1000;
  55.                 back_right.motorTorque = -1000;
  56.             }
  57.             else
  58.             {
  59.                 back_left.brakeTorque = 5000;
  60.                 back_right.brakeTorque = 5000;
  61.             }
  62.         }
  63.         if (CrossPlatformInputManager.GetButton("Jump"))
  64.         {
  65.             freinge = true;
  66.             BackLight.SetActive(true);
  67.             back_left.brakeTorque = Mathf.Infinity;
  68.             back_right.brakeTorque = Mathf.Infinity;
  69.             front_left.brakeTorque = Mathf.Infinity;
  70.             front_right.brakeTorque = Mathf.Infinity;
  71.             back_left.motorTorque = 0;
  72.             back_right.motorTorque = 0;
  73.         }
  74.         else
  75.         {
  76.             freinge = false;
  77.             BackLight.SetActive(false);
  78.         }
  79.         if (CrossPlatformInputManager.GetAxis("Horizontal") < 0)
  80.         {
  81.             front_right.brakeTorque = 0;
  82.             front_left.brakeTorque = 0;
  83.             back_left.brakeTorque = 0;
  84.             back_right.brakeTorque = 0;
  85.             back_left.motorTorque = CrossPlatformInputManager.GetAxis("Vertical") * Torque * coefAcceleration * Time.deltaTime;
  86.             back_right.motorTorque = CrossPlatformInputManager.GetAxis("Vertical") * Torque * coefAcceleration * Time.deltaTime;
  87.         }
  88.  
  89.         float DA = (((WheelAngleMax - DAmax) / MaxSpeed) * speed) + DAmax;
  90.         front_left.steerAngle = CrossPlatformInputManager.GetAxis("Horizontal") * DA;
  91.         front_right.steerAngle = CrossPlatformInputManager.GetAxis("Horizontal") * DA;
  92.  
  93.         FL.Rotate(front_left.rpm / 60 * 360 * Time.deltaTime, 0, 0);
  94.         FR.Rotate(front_right.rpm / 60 * 360 * Time.deltaTime, 0, 0);
  95.         BL.Rotate(back_left.rpm / 60 * 360 * Time.deltaTime, 0, 0);
  96.         BR.Rotate(back_right.rpm / 60 * 360 * Time.deltaTime, 0, 0);
  97.  
  98.         FL.localEulerAngles = new Vector3(FL.localEulerAngles.x, front_left.steerAngle - FL.localEulerAngles.z, FL.localEulerAngles.z);
  99.         FR.localEulerAngles = new Vector3(FR.localEulerAngles.x, front_right.steerAngle - FR.localEulerAngles.z, FR.localEulerAngles.z);
  100.  
  101.  
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement