Matias_Gray

Untitled

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