Advertisement
Cassimus

Drive Support

Oct 6th, 2023
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | Writing | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class DriveSupport : MonoBehaviour
  6. {
  7.     private Rigidbody bodyCar;
  8.     private float lastTimeChecked;
  9.     [SerializeField] float antiRoll = 5000;
  10.     [SerializeField] Wheel[] frontWhees;
  11.     [SerializeField] Wheel[] rearWheels;
  12.  
  13.     private void Awake()
  14.     {
  15.         bodyCar = GetComponent<Rigidbody>();
  16.     }
  17.  
  18.     private void Update()
  19.     {
  20.         if (transform.up.y > 0.5f || bodyCar.velocity.magnitude > 1)
  21.             lastTimeChecked = Time.time;
  22.  
  23.         if (Time.time > lastTimeChecked + 3) TurnBackCar();
  24.     }
  25.     private void FixedUpdate()
  26.     {
  27.         HoldWheelOnGround(frontWhees);
  28.         HoldWheelOnGround(rearWheels);
  29.     }
  30.  
  31.     private void TurnBackCar()
  32.     {
  33.         transform.position += Vector3.up;
  34.  
  35.         transform.rotation = Quaternion.LookRotation(transform.forward);
  36.     }
  37.  
  38.     private void HoldWheelOnGround(Wheel[] wheels)
  39.     {
  40.         Dictionary<Side, float> suspention = new Dictionary<Side, float>();
  41.         suspention.Clear();
  42.         suspention = CreateSuspention(wheels);
  43.  
  44.         float antiRollForce = (suspention[Side.Left] - suspention[Side.Right]) * antiRoll;
  45.  
  46.         foreach (var wheel in wheels)
  47.         {
  48.             WheelCollider wheelCollider = wheel.GetWheelCollider();
  49.             if (IsGrounded(wheelCollider, out WheelHit hit))
  50.             {
  51.                 var force = wheelCollider.transform.up * antiRollForce;
  52.                 if (wheel.GetSide() == Side.Left)
  53.                     force = -force;
  54.                 bodyCar.AddForceAtPosition(force, wheel.transform.position);
  55.             }
  56.         }
  57.  
  58.     }
  59.  
  60.     private Dictionary<Side, float> CreateSuspention(Wheel[] wheels)
  61.     {
  62.         Dictionary<Side, float> suspentionForceFactor = new Dictionary<Side, float>();
  63.  
  64.         foreach (var wheel in wheels)
  65.         {
  66.             WheelCollider wheelCollider = wheel.GetWheelCollider();
  67.  
  68.             if (IsGrounded(wheelCollider, out WheelHit hit))
  69.             {
  70.                 suspentionForceFactor[wheel.GetSide()] =
  71.                     (-wheelCollider.transform.InverseTransformPoint(hit.point).y -
  72.                     wheelCollider.radius) / wheelCollider.suspensionDistance;
  73.                 Debug.Log(-wheelCollider.transform.InverseTransformPoint(hit.point).y -
  74.                         wheelCollider.radius);
  75.             }
  76.             else
  77.                 suspentionForceFactor[wheel.GetSide()] = 1.0f;
  78.         }
  79.         return suspentionForceFactor;
  80.     }
  81.  
  82.     private bool IsGrounded(WheelCollider wheel, out WheelHit hit)
  83.     {
  84.         return wheel.GetGroundHit(out hit);
  85.     }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement