Добавил открытие элементов кузова
This commit is contained in:
1 parent
66ddcda5d6
commit
a33b4cd5f5
15 files changed
+1390
-97
No files matched your search
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6f2b1b4674e74a4ebfa713844c5ed0dd
|
||||
timeCreated: 1789942925
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 37ba02caa32943f69ac40c67217aec57
|
||||
timeCreated: 1789943104
|
||||
@@ -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
|
||||
@@ -0,0 +1,18 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
public class CarPartInteractable : MonoBehaviour, IInteractable
|
||||
{
|
||||
[SerializeField] private string promptMessage = "Открыть (E)";
|
||||
|
||||
// Чистое C# событие
|
||||
public event Action OnInteract;
|
||||
|
||||
public string GetPrompt() => promptMessage;
|
||||
|
||||
public void Interact(GameObject interactor)
|
||||
{
|
||||
// Вызываем событие, если на него кто-то подписан
|
||||
OnInteract?.Invoke();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b3e2d09be7614eb280964996788872ed
|
||||
timeCreated: 1789942997
|
||||
-1
@@ -1,6 +1,5 @@
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(CarController))]
|
||||
public class VehicleInteraction : MonoBehaviour, IInteractable
|
||||
{
|
||||
[Header("Cameras & Setup")]
|
||||
File renamed without changes.
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 631bb0dbce1e4eff852b0101516439ac
|
||||
timeCreated: 1789942959
|
||||
@@ -2,7 +2,6 @@ using UnityEngine;
|
||||
|
||||
public enum DriveType { FWD, RWD, AWD }
|
||||
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
public class CarController : MonoBehaviour
|
||||
{
|
||||
public float SpeedKmH => rb.linearVelocity.magnitude * 3.6f;
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
Reference in new issue
Block a user