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
- {
- [SerializeField] float horsePower = 0f;
- private float turnSpeed = 50.0f;
- private float horizontalInput;
- private float forwardInput;
- [SerializeField] GameObject centerOfMass;
- //speedometer
- [SerializeField] TextMeshProUGUI speedometerText;
- [SerializeField] float speed;
- //RPM
- [SerializeField] TextMeshProUGUI rpmText;
- [SerializeField] float rpm;
- //camera switch
- public Camera mainCamera;
- public Camera hoodCamera;
- public KeyCode switchKey;
- //2 player
- public string inputID;
- [SerializeField] List<WheelCollider> allWheels;
- [SerializeField] int wheelsOnGround;
- private Rigidbody playerRb;
- private void Start()
- {
- playerRb = GetComponent<Rigidbody>();
- playerRb.centerOfMass = centerOfMass.transform.position;
- }
- void FixedUpdate()
- {
- //player input
- //control left/right movement
- horizontalInput = Input.GetAxis("Horizontal" + inputID);
- //control forward and back movement
- forwardInput = Input.GetAxis("Vertical" + inputID);
- 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);
- }
- if (Input.GetKeyDown(switchKey))
- {
- mainCamera.enabled = !mainCamera.enabled;
- hoodCamera.enabled = !hoodCamera.enabled;
- }
- }
- 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