2
0

WheelConfig.cs 1.6 KB

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