Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using TMPro;
- public enum DrivetrainType
- {
- FWD,
- RWD,
- }
- public class CarMovement : MonoBehaviour
- {
- [SerializeField] private UIController _UIController;
- [SerializeField] DrivetrainType drivetrainType;
- private Rigidbody rb;
- [SerializeField] WheelCollider wheelFL;
- [SerializeField] WheelCollider wheelFR;
- [SerializeField] WheelCollider wheelRL;
- [SerializeField] WheelCollider wheelRR;
- private float rbVelocityLimit = 15f;
- [Range(300f, 900f)]
- [SerializeField] private float accelartionStraight;
- private float currentYRotation;
- private float targetYRotation;
- public bool IsActive;
- [SerializeField] private AnimationCurve curve;
- [SerializeField] private TrailRenderer trailRendererRR;
- [SerializeField] private TrailRenderer trailRendererRL;
- private Trail trailRR;
- private Trail trailRL;
- private float currentScore;
- public float CurrentScore
- {
- get
- {
- return currentScore;
- }
- set
- {
- currentScore = value;
- _UIController.UpdateScoreView((int)value);
- RaceLoop.Instance.Score = (int)value;
- }
- }
- [SerializeField] private float driftScoreFactor;
- private float horizontalForce, verticalForce;
- private void Awake()
- {
- rb = GetComponent<Rigidbody>();
- trailRL = trailRendererRL.GetComponent<Trail>();
- trailRR = trailRendererRR.GetComponent<Trail>();
- wheelFL.transform.localEulerAngles = Vector3.zero;
- wheelFR.transform.localEulerAngles = Vector3.zero;
- }
- public void UpdateInputAxis(float horizontal, float vertical)
- {
- horizontalForce = horizontal;
- verticalForce = vertical;
- }
- private void FixedUpdate()
- {
- if (!IsActive)
- {
- PerformBrake(1f);
- trailRendererRL.emitting = false;
- trailRendererRR.emitting = false;
- return;
- }
- float currentVelocity = rb.linearVelocity.magnitude;
- targetYRotation = horizontalForce * curve.Evaluate(currentVelocity);
- RotateWheels(targetYRotation);
- wheelFL.steerAngle = targetYRotation;
- wheelFR.steerAngle = targetYRotation;
- currentYRotation = targetYRotation;
- if(verticalForce > 0)
- {
- ResetBrakes();
- if (drivetrainType == DrivetrainType.FWD)
- {
- wheelFL.motorTorque = verticalForce * accelartionStraight * (2.8f - currentVelocity / rbVelocityLimit);
- wheelFR.motorTorque = verticalForce * accelartionStraight * (2.8f - currentVelocity / rbVelocityLimit);
- wheelRL.motorTorque = verticalForce * accelartionStraight * (2.02f - currentVelocity / rbVelocityLimit);
- wheelRR.motorTorque = verticalForce * accelartionStraight * (2.02f - currentVelocity / rbVelocityLimit);
- }
- else if (drivetrainType == DrivetrainType.RWD)
- {
- wheelFL.motorTorque = verticalForce * accelartionStraight * (2.02f - currentVelocity / rbVelocityLimit);
- wheelFR.motorTorque = verticalForce * accelartionStraight * (2.02f - currentVelocity / rbVelocityLimit);
- wheelRL.motorTorque = verticalForce * accelartionStraight * (2.8f - currentVelocity / rbVelocityLimit);
- wheelRR.motorTorque = verticalForce * accelartionStraight * (2.8f - currentVelocity / rbVelocityLimit);
- }
- }
- else {
- PerformBrake(0.05f);
- }
- float moveDirection = Vector3.Dot(transform.forward, rb.linearVelocity);
- if (moveDirection < -0.5f && verticalForce > 0)
- {
- PerformBrake(Mathf.Abs(verticalForce));
- }
- else if (moveDirection > 0.5f && verticalForce < 0)
- {
- PerformBrake(Mathf.Abs(verticalForce));
- }
- else if (verticalForce < 0)
- {
- PerformMoveBackwards(verticalForce);
- }
- HandledDriftData();
- }
- private void HandledDriftData()
- {
- float driftDirection = Vector3.Dot(transform.right, rb.linearVelocity);
- int driftValue = Mathf.Abs((int)driftDirection);
- if (driftValue > 3 && trailRL.IsGrounded && trailRR.IsGrounded)
- {
- trailRendererRL.emitting = true;
- trailRendererRR.emitting = true;
- CurrentScore += Time.deltaTime * driftScoreFactor;
- }
- else
- {
- trailRendererRL.emitting = false;
- trailRendererRR.emitting = false;
- }
- }
- private void ResetBrakes()
- {
- wheelFL.brakeTorque = 0f;
- wheelFR.brakeTorque = 0f;
- wheelRL.brakeTorque = 0f;
- wheelRR.brakeTorque = 0f;
- }
- private void PerformBrake(float force)
- {
- wheelFL.brakeTorque = force * 150f * 0.3f * accelartionStraight;
- wheelFR.brakeTorque = force * 150f * 0.3f * accelartionStraight;
- wheelRL.brakeTorque = force * 150f * 0.7f * accelartionStraight;
- wheelRR.brakeTorque = force * 150f * 0.7f * accelartionStraight;
- }
- private void PerformMoveBackwards(float force)
- {
- ResetBrakes();
- wheelFL.motorTorque = force * accelartionStraight * 0.4f;
- wheelFR.motorTorque = force * accelartionStraight * 0.4f;
- wheelRL.motorTorque = force * accelartionStraight * 0.4f;
- wheelRR.motorTorque = force * accelartionStraight * 0.4f;
- }
- private void RotateWheels(float targetYAngle)
- {
- wheelFL.transform.localEulerAngles = new Vector3(wheelFL.transform.localEulerAngles.x, targetYAngle, wheelFL.transform.localEulerAngles.z);
- wheelFR.transform.localEulerAngles = new Vector3(wheelFR.transform.localEulerAngles.x, targetYAngle, wheelFR.transform.localEulerAngles.z);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement