using System.Collections; using UnityEngine; [RequireComponent(typeof(CarPart))] public class CarPartAnimator : MonoBehaviour { [Header("Настройки открытия")] [SerializeField] private Vector3 openRotation; // Например: (0, 0, 60) для капота [SerializeField] private float speed = 5f; public bool IsOpen => isOpen; private Quaternion closedRot; private Quaternion openRot; private bool isOpen = false; private Coroutine animationCoroutine; private void Awake() { closedRot = transform.localRotation; // Конвертируем введенные углы Эйлера в Кватернион openRot = closedRot * Quaternion.Euler(openRotation); } public void TogglePart() { isOpen = !isOpen; // Если деталь уже двигалась, останавливаем старую корутину, чтобы не было конфликтов if (animationCoroutine != null) StopCoroutine(animationCoroutine); animationCoroutine = StartCoroutine(AnimatePart(isOpen ? openRot : closedRot)); } public void ResetToClosed() { isOpen = false; if (animationCoroutine != null) StopCoroutine(animationCoroutine); transform.localRotation = closedRot; } private IEnumerator AnimatePart(Quaternion targetRot) { // Пока угол между текущим и целевым вращением больше 0.1 градуса while (Quaternion.Angle(transform.localRotation, targetRot) > 0.1f) { // Плавно вращаем деталь transform.localRotation = Quaternion.Lerp(transform.localRotation, targetRot, Time.deltaTime * speed); yield return null; // Ждем следующего кадра } // Принудительно доводим до идеального угла в конце transform.localRotation = targetRot; } }