drakon-firestone

DrivingScript

Oct 14th, 2021
653
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class DrivingScript : MonoBehaviour
  6. {
  7.     public WheelScript[] wheels;
  8.     public float torque = 200;
  9.     public float maxSteerAngle = 30;
  10.     public float maxBrakeTorque = 500;
  11.     public float maxSpeed = 200;
  12.     public Rigidbody rb;
  13.     public float currentSpeed;
  14.  
  15.     // Update is called once per frame
  16.     void Update()
  17.     {
  18.        
  19.     }
  20.  
  21.     public void Drive(float accel, float brake, float steer)
  22.     {
  23.         accel = Mathf.Clamp(accel, -1, 1);
  24.         steer = Mathf.Clamp(steer, -1, 1) * maxSteerAngle;
  25.         brake = Mathf.Clamp(brake, 0, 1) * maxBrakeTorque;
  26.  
  27.         float thrustTorque = 0;
  28.         currentSpeed = rb.velocity.magnitude * 3;
  29.         if (currentSpeed < maxSpeed)
  30.         {
  31.             thrustTorque = accel * torque;
  32.         }
  33.        
  34.         foreach(WheelScript wheel in wheels)
  35.         {
  36.             wheel.wheelCollider.motorTorque = thrustTorque;
  37.             if(wheel.frontWheel)
  38.             {
  39.                 wheel.wheelCollider.steerAngle = steer;
  40.             }
  41.             else
  42.             {
  43.                 wheel.wheelCollider.brakeTorque = brake;
  44.             }
  45.  
  46.             Quaternion quat;
  47.             Vector3 position;
  48.             wheel.wheelCollider.GetWorldPose(out position, out quat);
  49.             wheel.wheel.transform.position = position;
  50.             wheel.wheel.transform.rotation = quat;
  51.         }        
  52.     }
  53. }
  54.  
RAW Paste Data