using UnityEngine; namespace WSMGameStudio.RailroadSystem { public class TrainWheelsTruck : MonoBehaviour { [SerializeField] private Transform _targetSteeringJoint; private Transform _transform; private Vector3 _targetRotation; private Vector3 _currentRotation; private float _speed = 1f; /// /// Start is called before the first frame update /// void Start() { _transform = GetComponent(); _targetRotation = Vector3.zero; } /// /// Update is called once per frame /// void Update() { UpdateWheelsTruckRotation(); } /// /// Updates rotation to follow tracks /// private void UpdateWheelsTruckRotation() { if (_targetSteeringJoint != null) { //calculate new rotation _transform.rotation = Quaternion.RotateTowards(_transform.rotation, _targetSteeringJoint.rotation, _speed); //Lock x and z axis, only y axis needs to be updated _targetRotation = _transform.localEulerAngles; _targetRotation.x = 0f; _targetRotation.z = 0f; _transform.localEulerAngles = _targetRotation; } } } }