WheelConfig.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using UnityEngine;
  2. [System.Serializable]
  3. public class WheelConfig
  4. {
  5. public WheelCollider frontLeft;
  6. public WheelCollider frontRight;
  7. public WheelCollider rearLeft;
  8. public WheelCollider rearRight;
  9. public WheelCollider[] AllWheels => new[] {frontLeft, frontRight, rearLeft, rearLeft};
  10. public void AdjustToGameObjects(Transform frontWheel, Transform rearWheel, float offset)
  11. {
  12. var x0 = Vector3.one - Vector3.right;
  13. var posFront = frontWheel.localPosition;
  14. var newXLeftFront = posFront.x - offset;
  15. var newXRightFront = posFront.x + offset;
  16. var posRear = rearWheel.localPosition;
  17. var newXLeftRear = posRear.x - offset;
  18. var newXRightRear = posRear.x + offset;
  19. SetNewX(frontLeft.transform, newXLeftFront);
  20. SetNewX(frontRight.transform, newXRightFront);
  21. SetNewX(rearLeft.transform, newXLeftRear);
  22. SetNewX(rearRight.transform, newXRightRear);
  23. }
  24. private void SetNewX(Transform transform, float newX)
  25. {
  26. var position = transform.localPosition;
  27. position = new Vector3(newX, position.y, position.z);
  28. transform.localPosition = position;
  29. }
  30. }