using UnityEngine; public class HookGrabber : MonoBehaviour { public Transform attachPoint; // 比如钩子底端的空物体 private GameObject candidateCargo; // 当前可被吸附的货物 private GameObject grabbedCargo; // 已吸附的货物 private BoxCollider Hookcollider; private float SizeZ; //private float Min = 0f; private void Start() { Hookcollider = GetComponent(); //Min = (6.295807f - 0.3099952f) / 100; SizeZ = 5.95f - 1.008f; } void Update() { // 按N键:吸附货物 if (candidateCargo != null && grabbedCargo == null && Input.GetKeyDown(KeyCode.N)) { grabbedCargo = candidateCargo; grabbedCargo.transform.SetParent(attachPoint); // grabbedCargo.transform.position = attachPoint.position + (SizeZ/2); Debug.Log("吸附到货物:" + grabbedCargo.name); } float currentValue = CraneController.Instance.hookRenderer.GetBlendShapeWeight(0); Debug.Log("currentValue:" + currentValue); Debug.Log("SizeZ:" + (SizeZ / 100 * (100 - currentValue))); Debug.Log("attachPoint.position.y:" + attachPoint.position.y); if (grabbedCargo != null) { grabbedCargo.transform.position = new Vector3(attachPoint.position.x, (attachPoint.position.y + (SizeZ / 100 * (100 - currentValue)) - 5.52f), attachPoint.position.z); } // 按M键:释放货物 if (grabbedCargo != null && Input.GetKeyDown(KeyCode.M)) { grabbedCargo.transform.SetParent(null); grabbedCargo = null; Debug.Log("货物已分离"); } } private void OnTriggerEnter(Collider other) { if (other.CompareTag("货物")) { candidateCargo = other.gameObject; Debug.Log("检测到货物:" + candidateCargo.name); } } private void OnTriggerExit(Collider other) { // 离开触发区后,如果没吸附,就清空候选 if (candidateCargo != null && other.gameObject == candidateCargo && grabbedCargo == null) { candidateCargo = null; Debug.Log("货物离开触发范围"); } } }