Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using TMPro;
- public class PlayerController : MonoBehaviour
- {
- //prevent driving in the air
- [SerializeField] List<WheelCollider> allWheels;
- [SerializeField] int wheelsOnGround;
- private Rigidbody playerRb;
- void FixedUpdate()
- {
- if (IsOnGround())
- {
- //commented out the old code to move forward and replaced with the new line below
- //transform.Translate(Vector3.forward * Time.deltaTime * horsePower * forwardInput);
- //Move the vehicle forward
- playerRb.AddRelativeForce(Vector3.forward * forwardInput * horsePower);
- //have vehicle rotate as it turns
- transform.Rotate(Vector3.up * Time.deltaTime * turnSpeed * horizontalInput);
- //speedometer
- speed = Mathf.Round(playerRb.velocity.magnitude * 3.6f);
- speedometerText.SetText("Speed: " + speed + "kph");
- //rpm
- rpm = Mathf.Round((speed % 30) * 40);
- rpmText.SetText("RPM: " + rpm);
- }
- }
- //prevent driving in the air
- bool IsOnGround()
- {
- wheelsOnGround = 0;
- foreach (WheelCollider wheel in allWheels)
- {
- if (wheel.isGrounded)
- {
- wheelsOnGround++;
- }
- }
- if (wheelsOnGround == 4)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement