using UnityEngine; using System.Collections; public class AttachOnCollision_QR2 : MonoBehaviour { public QuickRope2 rope; //public string connectableTag = "Player"; public KeyCode detachKey = KeyCode.Space; public Transform checkDisPoint;//检查碰撞时目标可吊点位与吊钩的距离 避免任意位置都能吊起的情况 //public GUIText HUD = null; private CraneInterGO attachedObject = null; public bool isConnected = false; void Update() { if (Input.GetKeyDown(detachKey)) { if (isConnected) //已经吊起物体时 放下物体 { rope.DetachObject(attachedObject.gameObject); attachedObject.UnSling(); //if (HUD) // HUD.text = "Hook Disconnected"; } else if(attachedObject) //满足吊起条件 空格吊起一个物体 { isConnected = true; rope.AttachObject(attachedObject.gameObject, rope.Joints.Count - 1, false); attachedObject.OnSling(); } } } void OnCollisionEnter(Collision col) { if (isConnected) return; CraneInterGO craneInterGO = col.transform.GetComponent(); if (!craneInterGO || !craneInterGO.isSlinged) return; if (Vector3.Distance(checkDisPoint.transform.position, craneInterGO.GetCraneAnchorPos()) >0.2f) { Debug.Log("起吊位置差过大"); return; } attachedObject = craneInterGO; //if (HUD) // HUD.text = "Hook Connected"; } private void OnCollisionExit(Collision collision) { if (attachedObject&&collision.gameObject == attachedObject.gameObject) { attachedObject = null; isConnected = false; } } }