Супер сырая система с заменой колес. Надо дорабатывать еще.
This commit is contained in:
1 parent
5f25a8eab4
commit
a497916ad0
1424 files changed
+6339
-91467
No files matched your search
@@ -19,26 +19,52 @@ public class CarPart : MonoBehaviour, IInteractable, ISecondaryInteractable
|
||||
private Vector3 originalLocalPos;
|
||||
private Quaternion originalLocalRot;
|
||||
private Vector3 originalLocalScale;
|
||||
|
||||
private WheelBolt[] wheelBolts;
|
||||
|
||||
private Rigidbody rb;
|
||||
private Collider partCollider;
|
||||
private GameObject currentMountZone;
|
||||
|
||||
private WheelCollider linkedWheelCollider;
|
||||
private Transform currentHoldPoint;
|
||||
private Vector3 targetInHandLocalPos;
|
||||
private Quaternion targetInHandLocalRot;
|
||||
|
||||
private float unboltDuration => (partData != null && partData.boltDuration > 0f) ? partData.boltDuration : 1.2f;
|
||||
|
||||
private float unboltProgress = 0f;
|
||||
public float UnboltProgress01 => Mathf.Clamp01(unboltProgress / unboltDuration);
|
||||
|
||||
public void ResetUnbolt() => unboltProgress = 0f;
|
||||
public CarPartData Data => partData;
|
||||
public PartState State => currentState;
|
||||
|
||||
public int TotalBoltsCount => wheelBolts != null ? wheelBolts.Length : 0;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
partCollider = GetComponent<Collider>();
|
||||
partAnimator = GetComponent<CarPartAnimator>();
|
||||
wheelBolts = GetComponentsInChildren<WheelBolt>(true);
|
||||
|
||||
originalParent = transform.parent;
|
||||
originalLocalPos = transform.localPosition;
|
||||
originalLocalRot = transform.localRotation;
|
||||
originalLocalScale = transform.localScale;
|
||||
|
||||
|
||||
|
||||
// Если эта деталь — колесо, автоматически находим её физический WheelCollider по имени (например, "Wheel_FL")
|
||||
VehicleInteraction car = GetComponentInParent<VehicleInteraction>();
|
||||
if (car != null)
|
||||
{
|
||||
foreach (WheelCollider wc in car.GetComponentsInChildren<WheelCollider>(true))
|
||||
{
|
||||
if (wc.name == gameObject.name)
|
||||
{
|
||||
linkedWheelCollider = wc;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string GetPrompt()
|
||||
@@ -47,7 +73,21 @@ public class CarPart : MonoBehaviour, IInteractable, ISecondaryInteractable
|
||||
|
||||
if (currentState == PartState.Installed)
|
||||
{
|
||||
// Если на детали есть аниматор — выводим подсказку с открытием
|
||||
if (wheelBolts != null && wheelBolts.Length > 0 && !CarJack.IsWheelLifted(transform))
|
||||
{
|
||||
return "Колесо под нагрузкой! Сначала поднимите авто домкратом";
|
||||
}
|
||||
|
||||
if (wheelBolts != null && wheelBolts.Length > 0 && TightenedBoltsCount > 0)
|
||||
{
|
||||
return $"Сначала открутите болты ({TightenedBoltsCount}/{wheelBolts.Length})";
|
||||
}
|
||||
|
||||
if (unboltProgress > 0f)
|
||||
{
|
||||
return $"Откручивание болтов ({partData.partName})... {Mathf.RoundToInt(UnboltProgress01 * 100f)}%";
|
||||
}
|
||||
|
||||
if (partAnimator != null)
|
||||
{
|
||||
string action = partAnimator.IsOpen ? "Закрыть" : "Открыть";
|
||||
@@ -61,7 +101,8 @@ public class CarPart : MonoBehaviour, IInteractable, ISecondaryInteractable
|
||||
{
|
||||
return $"Взять {partData.partName} (E)";
|
||||
}
|
||||
|
||||
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
@@ -111,54 +152,69 @@ public class CarPart : MonoBehaviour, IInteractable, ISecondaryInteractable
|
||||
|
||||
hands.PickUp(this);
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
// Блокируем попытки скрипта подвески (EasySuspension) вернуть снятое колесо обратно на машину
|
||||
if (currentState == PartState.InHand && currentHoldPoint != null)
|
||||
{
|
||||
transform.localPosition = targetInHandLocalPos;
|
||||
transform.localRotation = targetInHandLocalRot;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnPickedUp(Transform holdPoint)
|
||||
{
|
||||
currentState = PartState.InHand;
|
||||
currentHoldPoint = holdPoint;
|
||||
|
||||
// Отключаем физику и коллайдер
|
||||
if (rb != null) Destroy(rb);
|
||||
partCollider.enabled = false;
|
||||
if (partCollider != null) partCollider.enabled = false;
|
||||
|
||||
// Если сняли колесо — выключаем его физический WheelCollider на машине
|
||||
if (linkedWheelCollider != null)
|
||||
linkedWheelCollider.enabled = false;
|
||||
|
||||
// 1. Привязываем к рукам и сохраняем оригинальный размер (чтобы не сплющило)
|
||||
transform.SetParent(holdPoint);
|
||||
transform.localScale = originalLocalScale;
|
||||
|
||||
// 2. Задаем поворот вперед с учетом возможной ручной поправки
|
||||
transform.localRotation = Quaternion.Euler(inHandRotationOffset);
|
||||
transform.localPosition = Vector3.zero;
|
||||
|
||||
// 3. АВТО-ЦЕНТРИРОВАНИЕ: компенсируем сбитый пивот геометрии
|
||||
Renderer meshRenderer = GetComponentInChildren<Renderer>();
|
||||
if (meshRenderer != null)
|
||||
{
|
||||
// Находим, где сейчас находится визуальный центр геометрии в мире
|
||||
Vector3 visualCenterWorld = meshRenderer.bounds.center;
|
||||
|
||||
// Вычисляем вектор сдвига, чтобы центр меша совпал с точкой HoldPoint
|
||||
Vector3 offsetToCenter = holdPoint.position - visualCenterWorld;
|
||||
|
||||
// Смещаем деталь так, чтобы её реальный центр оказался в руках
|
||||
transform.position += offsetToCenter;
|
||||
|
||||
// Применяем ручной оффсет, если захотите опустить деталь чуть ниже
|
||||
transform.localPosition += inHandPositionOffset;
|
||||
}
|
||||
|
||||
// Запоминаем вычисленную позицию в руках для удержания в LateUpdate
|
||||
targetInHandLocalPos = transform.localPosition;
|
||||
targetInHandLocalRot = transform.localRotation;
|
||||
}
|
||||
|
||||
public void OnDropped(Vector3 throwForce)
|
||||
{
|
||||
currentState = PartState.Dropped;
|
||||
|
||||
// Отвязываем от игрока
|
||||
// Гарантируем, что деталь вылетает именно из точки рук перед игроком, а не из арки авто
|
||||
if (currentHoldPoint != null)
|
||||
{
|
||||
transform.position = currentHoldPoint.position + currentHoldPoint.forward * 0.4f;
|
||||
}
|
||||
currentHoldPoint = null;
|
||||
|
||||
transform.SetParent(null);
|
||||
transform.localScale = originalLocalScale;
|
||||
|
||||
// Включаем коллайдер обратно
|
||||
partCollider.enabled = true;
|
||||
if (partCollider is MeshCollider meshCol) meshCol.convex = true;
|
||||
if (partCollider != null)
|
||||
{
|
||||
partCollider.enabled = true;
|
||||
if (partCollider is MeshCollider meshCol) meshCol.convex = true;
|
||||
}
|
||||
|
||||
// Включаем физику
|
||||
rb = gameObject.AddComponent<Rigidbody>();
|
||||
rb.mass = mass;
|
||||
rb.interpolation = RigidbodyInterpolation.Interpolate;
|
||||
@@ -172,17 +228,22 @@ public class CarPart : MonoBehaviour, IInteractable, ISecondaryInteractable
|
||||
public void Install()
|
||||
{
|
||||
currentState = PartState.Installed;
|
||||
currentHoldPoint = null;
|
||||
|
||||
if (rb != null) Destroy(rb);
|
||||
|
||||
// Возвращаем на исходное место на машине
|
||||
transform.SetParent(originalParent);
|
||||
transform.localPosition = originalLocalPos;
|
||||
transform.localRotation = originalLocalRot;
|
||||
transform.localScale = originalLocalScale;
|
||||
|
||||
partCollider.enabled = true;
|
||||
if (partCollider != null) partCollider.enabled = true;
|
||||
|
||||
// Включаем обратно физический WheelCollider при установке колеса
|
||||
if (linkedWheelCollider != null)
|
||||
linkedWheelCollider.enabled = true;
|
||||
}
|
||||
|
||||
|
||||
private void CreateMountZone()
|
||||
{
|
||||
@@ -197,4 +258,43 @@ public class CarPart : MonoBehaviour, IInteractable, ISecondaryInteractable
|
||||
CarPartMountZone zone = currentMountZone.AddComponent<CarPartMountZone>();
|
||||
zone.Initialize(this);
|
||||
}
|
||||
|
||||
public void ProcessUnbolting(GameObject interactor, float deltaTime)
|
||||
{
|
||||
if (wheelBolts != null && wheelBolts.Length > 0 && !CarJack.IsWheelLifted(transform)) return;
|
||||
|
||||
if (currentState != PartState.Installed) return;
|
||||
|
||||
if (wheelBolts != null && wheelBolts.Length > 0 && TightenedBoltsCount > 0) return;
|
||||
|
||||
// Если у детали есть отдельные болты и они УЖЕ выкручены — снимаем колесо сразу без повторного таймера!
|
||||
if (wheelBolts != null && wheelBolts.Length > 0)
|
||||
{
|
||||
TakeToHands(interactor);
|
||||
return;
|
||||
}
|
||||
|
||||
// Не даем снимать детали, если в руках шлифмашинка или баллончик
|
||||
PlayerToolManager toolMgr = interactor.GetComponentInChildren<PlayerToolManager>();
|
||||
if (toolMgr != null && toolMgr.currentTool != ToolType.Hands) return;
|
||||
|
||||
unboltProgress += deltaTime;
|
||||
if (unboltProgress >= unboltDuration)
|
||||
{
|
||||
unboltProgress = 0f;
|
||||
TakeToHands(interactor);
|
||||
}
|
||||
}
|
||||
|
||||
public int TightenedBoltsCount
|
||||
{
|
||||
get
|
||||
{
|
||||
if (wheelBolts == null || wheelBolts.Length == 0) return 0;
|
||||
int count = 0;
|
||||
foreach (var b in wheelBolts)
|
||||
if (b != null && b.IsTightened) count++;
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user