Добавил открытие элементов кузова
This commit is contained in:
1 parent
66ddcda5d6
commit
a33b4cd5f5
15 files changed
+1390
-97
No files matched your search
@@ -0,0 +1,63 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(CarPartInteractable))]
|
||||
public class CarPartAnimator : MonoBehaviour
|
||||
{
|
||||
[Header("Настройки открытия")]
|
||||
[SerializeField] private Vector3 openRotation; // Например: (0, 0, 60) для капота
|
||||
[SerializeField] private float speed = 5f;
|
||||
|
||||
private CarPartInteractable interactable;
|
||||
private Quaternion closedRot;
|
||||
private Quaternion openRot;
|
||||
private bool isOpen = false;
|
||||
private Coroutine animationCoroutine;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
interactable = GetComponent<CarPartInteractable>();
|
||||
|
||||
// Запоминаем изначальное (закрытое) положение детали
|
||||
closedRot = transform.localRotation;
|
||||
// Конвертируем введенные углы Эйлера в Кватернион
|
||||
openRot = Quaternion.Euler(openRotation);
|
||||
}
|
||||
|
||||
// Подписываемся при включении объекта
|
||||
private void OnEnable()
|
||||
{
|
||||
interactable.OnInteract += TogglePart;
|
||||
}
|
||||
|
||||
// ОТПИСЫВАЕМСЯ при выключении/удалении (Критически важно!)
|
||||
private void OnDisable()
|
||||
{
|
||||
interactable.OnInteract -= TogglePart;
|
||||
}
|
||||
|
||||
private void TogglePart()
|
||||
{
|
||||
isOpen = !isOpen;
|
||||
|
||||
// Если деталь уже двигалась, останавливаем старую корутину, чтобы не было конфликтов
|
||||
if (animationCoroutine != null)
|
||||
StopCoroutine(animationCoroutine);
|
||||
|
||||
animationCoroutine = StartCoroutine(AnimatePart(isOpen ? openRot : 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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9aac6d89a0a64dc6ba41ec26a0117e79
|
||||
timeCreated: 1789943180
|
||||
Reference in new issue
Block a user