ZhangZhouSpecialEquipment/Assets/Scripts/门式起重机/MenShiHook.cs

86 lines
2.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MenShiHook : MonoBehaviour
{
private GameObject candidateCargo; // 当前可被吸附的货物
private GameObject grabbedCargo; // 已吸附的货物
public bool _isCanGet=false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//如果钩子被放到最底下了
if (MenShiManager.Instance.currentBlendValue > 99)
{
_isCanGet = true;
}
else
{
_isCanGet = false;
}
if(grabbedCargo != null)
{
float zValue = -0.16f + ((100-MenShiManager.Instance.currentBlendValue) / 100) * 0.142f;
grabbedCargo.transform.localPosition = new Vector3(grabbedCargo.transform.localPosition.x, grabbedCargo.transform.localPosition.y, zValue);
}
}
private void OnTriggerEnter(Collider other)
{
Debug.Log("触碰了");
if (other.CompareTag("货物"))
{
candidateCargo = other.gameObject;
Debug.Log("检测到货物:" + candidateCargo.name);
}
}
private void OnTriggerExit(Collider other)
{
Debug.Log("离开了");
// 离开触发区后,如果没吸附,就清空候选
if (candidateCargo != null && other.gameObject == candidateCargo && grabbedCargo == null)
{
candidateCargo = null;
Debug.Log("货物离开触发范围");
}
}
public void GrabCargo()
{
if (candidateCargo != null && grabbedCargo == null && _isCanGet)
{
grabbedCargo = candidateCargo;
grabbedCargo.transform.SetParent(transform);
// grabbedCargo.transform.position = attachPoint.position + (SizeZ/2);
Debug.Log("吸附到货物:" + grabbedCargo.name);
}
}
// 释放货物 (对应 M 键)
public void ReleaseCargo()
{
if (grabbedCargo != null && _isCanGet)
{
grabbedCargo.transform.SetParent(null);
grabbedCargo = null;
Debug.Log("货物已分离");
}
}
}