WheelConfig.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. var transform = frontLeft.transform;
  20. var localPosition = transform.localPosition;
  21. var newY = localPosition.y;
  22. var newZFront = localPosition.z;
  23. var newZRear = rearLeft.transform.localPosition.z;
  24. SetNewCoords(transform, newXLeftFront, newY, newZFront);
  25. SetNewCoords(frontRight.transform, newXRightFront, newY, newZFront);
  26. SetNewCoords(rearLeft.transform, newXLeftRear, newY, newZRear);
  27. SetNewCoords(rearRight.transform, newXRightRear, newY, newZRear);
  28. }
  29. private void SetNewCoords(Transform transform, float newX, float newY, float newZ)
  30. {
  31. transform.localPosition = new Vector3(newX, newY, newZ);
  32. }
  33. }