Stardog

CarS.cs

Oct 31st, 2017
3,067
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.77 KB | None | 0 0
  1. //http://docs.unity3d.com/Manual/WheelColliderTutorial.html
  2.  
  3. using UnityEngine;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6.  
  7. [System.Serializable]
  8. public class AxleInfo
  9. {
  10.     [Header("Settings")]
  11.     public bool motor;
  12.     public bool steering;
  13.     public bool hasBrakes;
  14.  
  15.     [Header("References")]
  16.     public WheelCollider leftWheel;
  17.     public WheelCollider rightWheel;
  18. }
  19.          
  20. public class CarS : MonoBehaviour {
  21.  
  22.     [Header("Info")]
  23.     public float currentSpeed;
  24.     public Vector3 currentVelocity;
  25.  
  26.     [Header("Settings")]
  27.     public List<AxleInfo> axleInfos;
  28.     public float maxMotorTorque = 400f;
  29.     public float maxSteeringAngle = 30f;
  30.     public float currentBrakeTorque = 0f;
  31.  
  32.     // COMPONENTS
  33.     private Rigidbody rb;
  34.  
  35.     //============================================
  36.     // FUNCTIONS (UNITY)
  37.     //============================================
  38.  
  39.     void Awake()
  40.     {
  41.         rb = GetComponent<Rigidbody>();
  42.     }
  43.  
  44.     void Update()
  45.     {
  46.         // BRAKE
  47.         if (Input.GetButton("Brake"))
  48.         {
  49.             currentBrakeTorque = 10000f;
  50.         }
  51.         else
  52.         {
  53.             currentBrakeTorque = 0f;
  54.         }
  55.  
  56.         // JUMP
  57.         if (Input.GetButtonDown("Jump"))
  58.         {
  59.             rb.AddForce(rb.centerOfMass + new Vector3(0f, 10000f, 0f), ForceMode.Impulse);
  60.         }
  61.  
  62.         // BOOST
  63.         if (Input.GetButton("Boost"))
  64.         {
  65.             rb.AddForce(transform.forward * 1000f, ForceMode.Impulse);
  66.         }
  67.     }
  68.  
  69.     void FixedUpdate()
  70.     {
  71.         // SPEED
  72.         currentVelocity = rb.velocity;
  73.         currentSpeed = rb.velocity.magnitude;
  74.  
  75.         // INPUT
  76.         float motor = maxMotorTorque * Input.GetAxis("Accelerator");
  77.         float steering = maxSteeringAngle * Input.GetAxis("Horizontal");
  78.  
  79.         foreach (AxleInfo axleInfo in axleInfos)
  80.         {
  81.             if (axleInfo.steering)
  82.             {
  83.                 axleInfo.leftWheel.steerAngle = steering;
  84.                 axleInfo.rightWheel.steerAngle = steering;
  85.             }
  86.  
  87.             if (axleInfo.motor)
  88.             {
  89.                 axleInfo.leftWheel.motorTorque = motor;
  90.                 axleInfo.rightWheel.motorTorque = motor;
  91.             }
  92.  
  93.             if (axleInfo.hasBrakes)
  94.             {
  95.                 axleInfo.leftWheel.brakeTorque = currentBrakeTorque;
  96.                 axleInfo.rightWheel.brakeTorque = currentBrakeTorque;
  97.             }
  98.  
  99.             ApplyLocalPositionToVisuals(axleInfo.leftWheel);
  100.             ApplyLocalPositionToVisuals(axleInfo.rightWheel);
  101.         }
  102.     }
  103.  
  104.     //============================================
  105.     // FUNCTIONS (CUSTOM)
  106.     //============================================
  107.  
  108.     // FINDS THE VISUAL WHEEL, CORRECTLY APPLIES THE TRANSFORM
  109.     public void ApplyLocalPositionToVisuals(WheelCollider collider)
  110.     {
  111.         if (collider.transform.childCount == 0) return;
  112.        
  113.         // GET WHEEL
  114.         Transform visualWheel = collider.transform.GetChild(0);
  115.        
  116.         // GET POS/ROT
  117.         Vector3 position;
  118.         Quaternion rotation;
  119.  
  120.         collider.GetWorldPose(out position, out rotation);
  121.  
  122.         // APPLY POS/ROT
  123.         visualWheel.transform.position = position;
  124.         visualWheel.transform.rotation = rotation;
  125.     }
  126.  
  127. }
Add Comment
Please, Sign In to add comment