// Use this for initialization void Start() { screwcrosssnappable = screwcross.gameObject.GetComponentInChildren <CommonSnappable> (); Debug.Log("The Snappable Scripts " + ((screwcrosssnappable == null) ? "is not found" : "is found")); originposition = screwcross.position; }
void OnTriggerExit(Collider snappablecoll) { if (snappable == null) { return; } else { CommonSnappable temp = snappablecoll.GetComponent <CommonSnappable> (); if (temp == snappable) { //Debug.Log ("snappable.gameObject: " + snappable.gameObject); //Debug.Log ("snappablecoll.gameObject: " + snappablecoll.gameObject); //Debug.Log (root.gameObject + "CommonSnapper OnTriggerExit"); Debug.Log("CommonSnapper OnTriggerExit"); enable = false; isSnapped = false; isSnapping = false; if (snappable != null) { snappable.isSnapped = false; snappable.isSnapping = false; snappable.snapper = null; snappable = null; } transform.rotation = root.rotation * snapperLocalRotation; transform.position = root.position + snapperLocalPosition; StartCoroutine(Reable(snapTimeInterval)); } else { return; } } }
void OnTriggerEnter(Collider snappablecoll) { //Debug.Log(root.gameObject + "CommonSnapper OnTriggerEnter"); Debug.Log("CommonSnapper OnTriggerEnter"); if (snappable == null) { if (snappablecoll.tag == "CommonSnappable") { snappable = snappablecoll.gameObject.GetComponent <CommonSnappable> (); if (snappable == null) { return; } if (snappable.enable == false || address != snappable.address) { snappable = null; return; } target = snappable.target; isSnapping = true; //Debug.Log (snappable.gameObject.transform.parent); snappable.isSnapping = true; snappable.snapper = this; } else { return; } } }
//Run with FixedUpdate, use Time.FixedDeltaTime void OnTriggerStay(Collider snappablecoll) { Debug.Log(root.gameObject + "CommonSnapper OnTriggerStay"); if (snappable == null) { if (snappablecoll.tag == "CommonSnappable") { snappable = snappablecoll.gameObject.GetComponent <CommonSnappable> (); if (snappable == null) { return; } if (snappable.enable == false || address != snappable.address) { snappable = null; return; } target = snappable.target; isSnapping = true; snappable.isSnapping = true; snappable.snapper = this; } else { return; } } if (enable == false) { return; } //Debug.Log ("isSnapped: " + isSnapped); //Debug.Log ("isSnapping: " + isSnapping); if (isSnapping && !isSnapped) { if (target == null) { return; } root.rotation = target.rotation * referenceRotationRelativeToRoot; root.position = target.position + referencePositionRelativeToRoot; Debug.Log("Distace: " + Vector3.Distance(reference.position, target.position)); if (Vector3.Distance(reference.position, target.position) < snappingDistanceMin) { Debug.Log("Distace: " + Vector3.Distance(reference.position, target.position)); isSnapping = false; snappable.isSnapping = false; isSnapped = true; snappable.isSnapped = true; } //Debug.Log ("equal rot: " + (reference.rotation == target.rotation)); //Debug.Log ("equal pos: " + (reference.position == target.position)); } //The parent stay but the reference move with collider if (!isSnapping && isSnapped) { //Debug.Log (root.gameObject + "CommonSnapper Enter Keeping mode"); Debug.Log("CommonSnapper Enter Keeping mode"); Vector3 temppos = transform.position; Quaternion temprot = transform.rotation; if (Vector3.Distance(reference.position, target.position) > snappingDistanceMin) { root.rotation = target.rotation * referenceRotationRelativeToRoot; root.position = target.position + referencePositionRelativeToRoot; Debug.Log("Move to keep"); } transform.position = temppos; transform.rotation = temprot; } }