Есть система снятия навесного с авто. Надо дорабатывать. Машины все добавлены
This commit is contained in:
1 parent
a8238ab49e
commit
3e13180e34
15 files changed
+106236
-141
No files matched your search
@@ -1,14 +1,14 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(CarPartInteractable))]
|
||||
[RequireComponent(typeof(CarPart))]
|
||||
public class CarPartAnimator : MonoBehaviour
|
||||
{
|
||||
[Header("Настройки открытия")]
|
||||
[SerializeField] private Vector3 openRotation; // Например: (0, 0, 60) для капота
|
||||
[SerializeField] private float speed = 5f;
|
||||
|
||||
private CarPartInteractable interactable;
|
||||
public bool IsOpen => isOpen;
|
||||
private Quaternion closedRot;
|
||||
private Quaternion openRot;
|
||||
private bool isOpen = false;
|
||||
@@ -16,27 +16,12 @@ public class CarPartAnimator : MonoBehaviour
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
interactable = GetComponent<CarPartInteractable>();
|
||||
|
||||
// Запоминаем изначальное (закрытое) положение детали
|
||||
closedRot = transform.localRotation;
|
||||
// Конвертируем введенные углы Эйлера в Кватернион
|
||||
openRot = Quaternion.Euler(openRotation);
|
||||
openRot = closedRot * Quaternion.Euler(openRotation);
|
||||
}
|
||||
|
||||
// Подписываемся при включении объекта
|
||||
private void OnEnable()
|
||||
{
|
||||
interactable.OnInteract += TogglePart;
|
||||
}
|
||||
|
||||
// ОТПИСЫВАЕМСЯ при выключении/удалении (Критически важно!)
|
||||
private void OnDisable()
|
||||
{
|
||||
interactable.OnInteract -= TogglePart;
|
||||
}
|
||||
|
||||
private void TogglePart()
|
||||
public void TogglePart()
|
||||
{
|
||||
isOpen = !isOpen;
|
||||
|
||||
@@ -46,6 +31,13 @@ public class CarPartAnimator : MonoBehaviour
|
||||
|
||||
animationCoroutine = StartCoroutine(AnimatePart(isOpen ? openRot : closedRot));
|
||||
}
|
||||
|
||||
public void ResetToClosed()
|
||||
{
|
||||
isOpen = false;
|
||||
if (animationCoroutine != null) StopCoroutine(animationCoroutine);
|
||||
transform.localRotation = closedRot;
|
||||
}
|
||||
|
||||
private IEnumerator AnimatePart(Quaternion targetRot)
|
||||
{
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b3e2d09be7614eb280964996788872ed
|
||||
timeCreated: 1789942997
|
||||
@@ -2,8 +2,7 @@ using UnityEngine;
|
||||
|
||||
public enum PartState { Installed, InHand, Dropped }
|
||||
|
||||
[RequireComponent(typeof(Collider))]
|
||||
public class CarPart : MonoBehaviour, IInteractable
|
||||
public class CarPart : MonoBehaviour, IInteractable, ISecondaryInteractable
|
||||
{
|
||||
[Header("Данные")]
|
||||
[SerializeField] private CarPartData partData;
|
||||
@@ -14,6 +13,7 @@ public class CarPart : MonoBehaviour, IInteractable
|
||||
[Tooltip("Поворот детали в руках (например, если бампер берется боком)")]
|
||||
[SerializeField] private Vector3 inHandRotationOffset = Vector3.zero;
|
||||
|
||||
private CarPartAnimator partAnimator;
|
||||
private PartState currentState = PartState.Installed;
|
||||
private Transform originalParent;
|
||||
private Vector3 originalLocalPos;
|
||||
@@ -31,6 +31,7 @@ public class CarPart : MonoBehaviour, IInteractable
|
||||
private void Awake()
|
||||
{
|
||||
partCollider = GetComponent<Collider>();
|
||||
partAnimator = GetComponent<CarPartAnimator>();
|
||||
|
||||
originalParent = transform.parent;
|
||||
originalLocalPos = transform.localPosition;
|
||||
@@ -44,15 +45,58 @@ public class CarPart : MonoBehaviour, IInteractable
|
||||
{
|
||||
if (partData == null) return "Деталь (E)";
|
||||
|
||||
return currentState switch
|
||||
if (currentState == PartState.Installed)
|
||||
{
|
||||
PartState.Installed => $"Снять {partData.partName} (E)",
|
||||
PartState.Dropped => $"Взять {partData.partName} (E)",
|
||||
_ => string.Empty
|
||||
};
|
||||
// Если на детали есть аниматор — выводим подсказку с открытием
|
||||
if (partAnimator != null)
|
||||
{
|
||||
string action = partAnimator.IsOpen ? "Закрыть" : "Открыть";
|
||||
return $"[E] {action} {partData.partName} | [R] Снять";
|
||||
}
|
||||
|
||||
return $"Снять {partData.partName} (E)";
|
||||
}
|
||||
|
||||
if (currentState == PartState.Dropped)
|
||||
{
|
||||
return $"Взять {partData.partName} (E)";
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public void Interact(GameObject interactor)
|
||||
{
|
||||
if (currentState == PartState.Installed)
|
||||
{
|
||||
if (partAnimator != null)
|
||||
{
|
||||
// Нажали E — открываем или закрываем
|
||||
partAnimator.TogglePart();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Обычная деталь (бампер) сразу снимается
|
||||
TakeToHands(interactor);
|
||||
}
|
||||
}
|
||||
else if (currentState == PartState.Dropped)
|
||||
{
|
||||
TakeToHands(interactor);
|
||||
}
|
||||
}
|
||||
|
||||
// Вызывается при нажатии R (вторичное действие)
|
||||
public void SecondaryInteract(GameObject interactor)
|
||||
{
|
||||
if (currentState == PartState.Installed)
|
||||
{
|
||||
TakeToHands(interactor);
|
||||
}
|
||||
}
|
||||
|
||||
// Вынесенный общий метод взятия в руки
|
||||
private void TakeToHands(GameObject interactor)
|
||||
{
|
||||
PlayerHands hands = interactor.GetComponent<PlayerHands>();
|
||||
if (hands == null) hands = interactor.GetComponentInParent<PlayerHands>();
|
||||
@@ -61,16 +105,11 @@ public class CarPart : MonoBehaviour, IInteractable
|
||||
|
||||
if (currentState == PartState.Installed)
|
||||
{
|
||||
// 1. Создаем зону для обратной установки на машине
|
||||
if (partAnimator != null) partAnimator.ResetToClosed();
|
||||
CreateMountZone();
|
||||
// 2. Берем сразу в руки
|
||||
hands.PickUp(this);
|
||||
}
|
||||
else if (currentState == PartState.Dropped)
|
||||
{
|
||||
// Подбираем деталь с земли
|
||||
hands.PickUp(this);
|
||||
}
|
||||
|
||||
hands.PickUp(this);
|
||||
}
|
||||
|
||||
public void OnPickedUp(Transform holdPoint)
|
||||
|
||||
Reference in new issue
Block a user