Advertisement
LeeMace

Full Driving Script

Dec 16th, 2022
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5.  
  6. public class PlayerController : MonoBehaviour
  7. {
  8.     [SerializeField] float horsePower = 0f;
  9.     private float turnSpeed = 50.0f;
  10.     private float horizontalInput;
  11.     private float forwardInput;
  12.     [SerializeField] GameObject centerOfMass;
  13.  
  14.     //speedometer
  15.     [SerializeField] TextMeshProUGUI speedometerText;
  16.     [SerializeField] float speed;
  17.  
  18.     //RPM
  19.     [SerializeField] TextMeshProUGUI rpmText;
  20.     [SerializeField] float rpm;
  21.  
  22.  
  23.     //camera switch
  24.     public Camera mainCamera;
  25.     public Camera hoodCamera;
  26.     public KeyCode switchKey;
  27.  
  28.     //2 player
  29.     public string inputID;
  30.  
  31.     [SerializeField] List<WheelCollider> allWheels;
  32.     [SerializeField] int wheelsOnGround;
  33.  
  34.     private Rigidbody playerRb;
  35.  
  36.     private void Start()
  37.     {
  38.         playerRb = GetComponent<Rigidbody>();
  39.         playerRb.centerOfMass = centerOfMass.transform.position;
  40.     }
  41.     void FixedUpdate()
  42.     {
  43.         //player input
  44.         //control left/right movement
  45.         horizontalInput = Input.GetAxis("Horizontal" + inputID);
  46.         //control forward and back movement
  47.         forwardInput = Input.GetAxis("Vertical" + inputID);
  48.  
  49.         if (IsOnGround())
  50.         {
  51.         //commented out the old code to move forward and replaced with the new line below
  52.         //transform.Translate(Vector3.forward * Time.deltaTime * horsePower * forwardInput);
  53.         //Move the vehicle forward
  54.         playerRb.AddRelativeForce(Vector3.forward * forwardInput * horsePower);
  55.         //have vehicle rotate as it turns
  56.         transform.Rotate(Vector3.up * Time.deltaTime * turnSpeed * horizontalInput);
  57.  
  58.         //speedometer
  59.         speed = Mathf.Round(playerRb.velocity.magnitude * 3.6f);
  60.         speedometerText.SetText("Speed: " + speed + "kph");
  61.  
  62.         //rpm
  63.         rpm = Mathf.Round((speed % 30) * 40);
  64.         rpmText.SetText("RPM: " + rpm);
  65.         }
  66.  
  67.         if (Input.GetKeyDown(switchKey))
  68.         {
  69.             mainCamera.enabled = !mainCamera.enabled;
  70.             hoodCamera.enabled = !hoodCamera.enabled;
  71.         }
  72.     }
  73.  
  74.     bool IsOnGround()
  75.     {
  76.         wheelsOnGround = 0;
  77.         foreach (WheelCollider wheel in allWheels)
  78.         {
  79.             if (wheel.isGrounded)
  80.             {
  81.                 wheelsOnGround++;
  82.             }
  83.         }
  84.         if (wheelsOnGround == 4)
  85.         {
  86.             return true;
  87.         }
  88.         else
  89.         {
  90.             return false;
  91.         }
  92.     }
  93. }
Tags: driving
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement