Matias_Gray

Untitled

Jul 29th, 2025
199
0
29 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.88 KB | None | 0 0
  1. using TMPro;
  2. using UnityEngine;
  3.  
  4. public class ShipVisual : MonoBehaviour
  5. {
  6.     [SerializeField] private float tiltMultiplier = 20;       // Насколько сильно наклоняется
  7.     [SerializeField] private float tiltSmoothness = 1.5f;       // Плавность крена
  8.  
  9.     private Rigidbody shipRigidbody;
  10.     private float previousYaw;
  11.     private float currentTilt = 0f;
  12.  
  13.     [SerializeField] private TextMeshProUGUI tiltText;
  14.    
  15.  
  16.     void Start()
  17.     {
  18.         shipRigidbody = GetComponentInParent<Rigidbody>();
  19.         if (shipRigidbody == null)
  20.         {
  21.             Debug.LogError("ShipVisual: Rigidbody not found in parent.");
  22.         }
  23.  
  24.         previousYaw = shipRigidbody.rotation.eulerAngles.y;
  25.     }
  26.  
  27.     void FixedUpdate()
  28.     {
  29.         float currentYaw = shipRigidbody.rotation.eulerAngles.y;
  30.         float deltaYaw = Mathf.DeltaAngle(previousYaw, currentYaw); // корректное вычисление угла
  31.         previousYaw = currentYaw;
  32.  
  33.         float targetTilt = deltaYaw * tiltMultiplier * Time.deltaTime; // знак "-" — вправо наклон при левом повороте
  34.         currentTilt = Mathf.Lerp(currentTilt, targetTilt, tiltSmoothness);
  35.         tiltText.text = $"FPS = {(1 / Time.deltaTime).ToString("F2")}\n" +
  36.                         $"currentYaw = {currentYaw.ToString("F2")}\n" +
  37.                         $"deltaYaw = {deltaYaw.ToString("F2")}\n" +
  38.                         $"targetTilt = {targetTilt.ToString("F2")}\n" +
  39.                         $"currentTilt = {currentTilt.ToString("F2")}";
  40.  
  41.         // Применяем крен только по оси Z, не трогая yaw
  42.         Quaternion baseRotation = Quaternion.Euler(0f, currentYaw, 0f);
  43.         Quaternion tiltRotation = Quaternion.Euler(0f, 0f, currentTilt);  
  44.         shipRigidbody.rotation = baseRotation * tiltRotation;
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment