Advertisement
MaxiBarometer

Car controller

Nov 30th, 2019
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.41 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(Rigidbody))]
  6. public class CarController : MonoBehaviour
  7. {
  8.     public float torque = 5000;
  9.     public float brakingTorque = 10000;
  10.     public float maxSteer = 45;
  11.  
  12.     public List<WheelCollider> backWheels;
  13.     public List<WheelCollider> frontWheels;
  14.     public List<Transform> wheelMeshes;
  15.     public List<Transform> springMeshes;
  16.  
  17.     public float downForce = 1;
  18.  
  19.     public float ForwardVelocity { private set; get; }
  20.  
  21.     List<WheelCollider> wheels;
  22.     Rigidbody rb;
  23.  
  24.     float throttle = 0.0f;
  25.     float braking = 0.0f;
  26.     float steering = 0.0f;
  27.     float minSteering = 0.1f;
  28.  
  29.     // Start is called before the first frame update
  30.     void Start()
  31.     {
  32.         wheels = new List<WheelCollider>();
  33.  
  34.         foreach (WheelCollider wheel in backWheels)
  35.             wheels.Add(wheel);
  36.  
  37.         foreach (WheelCollider wheel in frontWheels)
  38.             wheels.Add(wheel);
  39.  
  40.         rb = GetComponent<Rigidbody>();
  41.     }
  42.  
  43.     // Update is called once per frame
  44.     void FixedUpdate()
  45.     {
  46.         throttle = Input.GetAxis("Vertical");
  47.         braking = Input.GetAxis("Jump");
  48.         steering = Mathf.Lerp(steering, Input.GetAxis("Horizontal") * Mathf.Max(minSteering, 1.0f / (0.04f * ForwardVelocity + 1.0f)), Time.deltaTime * 10);
  49.  
  50.  
  51.         foreach (WheelCollider wheel in backWheels)
  52.         {
  53.             wheel.motorTorque = throttle * torque;
  54.         }
  55.  
  56.         ForwardVelocity = rb.velocity.magnitude * 3.6f;
  57.  
  58.         for (int i = 0; i < 2; ++i)
  59.         {
  60.             WheelCollider wheel = frontWheels[i];
  61.             wheel.steerAngle = steering * maxSteer;
  62.             springMeshes[i + 2].localRotation = Quaternion.AngleAxis(steering * maxSteer, Vector3.up);
  63.         }
  64.  
  65.         Ray downRay;
  66.         RaycastHit hit;
  67.         for (int i = 0; i < 4; ++i)
  68.         {
  69.             WheelCollider wheel = wheels[i];
  70.             wheelMeshes[i].Rotate(wheel.rpm / 60.0f * Time.deltaTime * 360.0f, 0, 0);
  71.             float distance = 0.2f;
  72.             if (wheel.isGrounded)
  73.             {
  74.                 downRay = new Ray(wheels[i].transform.position, -transform.up);
  75.                 if (Physics.Raycast(downRay, out hit, 1.0f, ~(1<<9)))
  76.                 {
  77.                     Debug.Log(hit.collider.gameObject.name);
  78.                     distance = hit.distance - 0.42f;
  79.                 }
  80.             }
  81.             distance = Mathf.Clamp(distance, 0.0f, 0.2f);
  82.             springMeshes[i].localPosition = new Vector3(springMeshes[i].localPosition.x, -distance, springMeshes[i].localPosition.z);
  83.         }
  84.  
  85.         foreach (WheelCollider wheel in wheels)
  86.             wheel.brakeTorque = braking * brakingTorque;
  87.  
  88.         rb.AddForce(-transform.up * downForce * ForwardVelocity);
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement