WheelConfig.cs 1.6 KB

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