103 lines
4.1 KiB
C#
103 lines
4.1 KiB
C#
using System.Collections;
|
|||
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Открытие дверей, капота и багажника.
|
||
|
|
/// Режим 1 (как раньше): вращение вокруг собственного пивота детали на openRotation.
|
||
|
|
/// Режим 2 (useHinge): вращение вокруг петли hingePoint/hingeAxis в координатах родителя —
|
||
|
|
/// используется автонастройкой для моделей, у которых пивот детали стоит в центре машины.
|
||
|
|
/// </summary>
|
||
|
|
[RequireComponent(typeof(CarPart))]
|
||
|
|
public class CarPartAnimator : MonoBehaviour
|
||
|
|
{
|
||
|
|
[Header("Настройки открытия")]
|
||
|
|
[SerializeField] private Vector3 openRotation; // Например: (0, 0, 60) для капота
|
||
|
|
[SerializeField] private float speed = 5f;
|
||
|
|
|
||
|
|
[Header("Петля (автонастройка)")]
|
||
|
|
[SerializeField] private bool useHinge;
|
||
|
|
[SerializeField] private Vector3 hingePoint;
|
||
|
|
[SerializeField] private Vector3 hingeAxis = Vector3.right;
|
||
|
|
[SerializeField] private float openAngle = -55f;
|
||
|
|
|
||
|
|
public bool IsOpen => isOpen;
|
||
|
|
private Quaternion closedRot;
|
||
|
|
private Quaternion openRot;
|
||
|
|
private Vector3 closedPos;
|
||
|
|
private bool isOpen = false;
|
||
|
|
private float openAmount;
|
||
|
|
private Coroutine animationCoroutine;
|
||
|
|
|
||
|
|
private void Awake()
|
||
|
|
{
|
||
|
|
closedRot = transform.localRotation;
|
||
|
|
closedPos = transform.localPosition;
|
||
|
|
// Конвертируем введенные углы Эйлера в Кватернион
|
||
|
|
openRot = closedRot * Quaternion.Euler(openRotation);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Настройка петли из Editor-автонастройки (координаты родителя детали).</summary>
|
||
|
|
public void ConfigureHinge(Vector3 pointInParent, Vector3 axisInParent, float angle, float animSpeed = 4f)
|
||
|
|
{
|
||
|
|
useHinge = true;
|
||
|
|
hingePoint = pointInParent;
|
||
|
|
hingeAxis = axisInParent.sqrMagnitude > 0.0001f ? axisInParent.normalized : Vector3.right;
|
||
|
|
openAngle = angle;
|
||
|
|
speed = animSpeed;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void TogglePart()
|
||
|
|
{
|
||
|
|
isOpen = !isOpen;
|
||
|
|
|
||
|
|
// Если деталь уже двигалась, останавливаем старую корутину, чтобы не было конфликтов
|
||
|
|
if (animationCoroutine != null)
|
||
|
|
StopCoroutine(animationCoroutine);
|
||
|
|
|
||
|
|
animationCoroutine = useHinge
|
||
|
|
? StartCoroutine(AnimateHinge(isOpen ? 1f : 0f))
|
||
|
|
: StartCoroutine(AnimatePart(isOpen ? openRot : closedRot));
|
||
|
|
}
|
||
|
|
|
||
|
|
public void ResetToClosed()
|
||
|
|
{
|
||
|
|
isOpen = false;
|
||
|
|
openAmount = 0f;
|
||
|
|
if (animationCoroutine != null) StopCoroutine(animationCoroutine);
|
||
|
|
transform.localRotation = closedRot;
|
||
|
|
if (useHinge) transform.localPosition = closedPos;
|
||
|
|
}
|
||
|
|
|
||
|
|
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;
|
||
|
|
}
|
||
|
|
|
||
|
|
private IEnumerator AnimateHinge(float target)
|
||
|
|
{
|
||
|
|
while (Mathf.Abs(openAmount - target) > 0.001f)
|
||
|
|
{
|
||
|
|
openAmount = Mathf.MoveTowards(openAmount, target, Time.deltaTime * speed * 0.35f);
|
||
|
|
ApplyHinge(Mathf.SmoothStep(0f, 1f, openAmount));
|
||
|
|
yield return null;
|
||
|
|
}
|
||
|
|
openAmount = target;
|
||
|
|
ApplyHinge(target);
|
||
|
|
}
|
||
|
|
|
||
|
|
private void ApplyHinge(float t)
|
||
|
|
{
|
||
|
|
Quaternion q = Quaternion.AngleAxis(openAngle * t, hingeAxis);
|
||
|
|
transform.localRotation = q * closedRot;
|
||
|
|
transform.localPosition = hingePoint + q * (closedPos - hingePoint);
|
||
|
|
}
|
||
|
|
}
|