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