Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class ShipVisual : MonoBehaviour
- {
- [SerializeField] private float tiltMultiplier = 50f; // Насколько сильно наклоняется
- [SerializeField] private float tiltSmoothness = 1f; // Плавность крена
- private Rigidbody shipRigidbody;
- private float previousYaw;
- private float currentTilt = 0f;
- void Start()
- {
- shipRigidbody = GetComponentInParent<Rigidbody>();
- if (shipRigidbody == null)
- {
- Debug.LogError("ShipVisual: Rigidbody not found in parent.");
- }
- previousYaw = transform.rotation.eulerAngles.y;
- }
- void Update()
- {
- if (shipRigidbody == null) return;
- float currentYaw = transform.rotation.eulerAngles.y;
- float deltaYaw = Mathf.DeltaAngle(previousYaw, currentYaw); // корректное вычисление угла
- previousYaw = currentYaw;
- float targetTilt = deltaYaw * tiltMultiplier; // знак "-" — вправо наклон при левом повороте
- currentTilt = Mathf.Lerp(currentTilt, targetTilt, Time.deltaTime * tiltSmoothness);
- // Применяем крен только по оси Z, не трогая yaw
- Quaternion baseRotation = Quaternion.Euler(0f, currentYaw, 0f);
- Quaternion tiltRotation = Quaternion.Euler(0f, 0f, currentTilt);
- transform.rotation = baseRotation * tiltRotation;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment