/// <summary> Add an object to this DropZone, and apply the desired settings. </summary> /// <param name="obj"></param> public void AddObject(SenseGlove_Grabable obj) { SenseGlove_Debugger.Log("Adding " + obj.name + " to the DropZone!"); // remember original parent if (obj.IsGrabbed()) { this.originalParent.Add(obj.GetOriginalParent()); } else { this.originalParent.Add(obj.transform.parent); } bool[] props = null; if (this.snapToMe && (this.takeFromHand || !obj.IsGrabbed())) { obj.EndInteraction(); Transform zoneParent = this.snapTarget; if (zoneParent == null) { zoneParent = this.gameObject.transform; } obj.gameObject.transform.parent = zoneParent; obj.gameObject.transform.localPosition = Vector3.zero; obj.gameObject.transform.localRotation = Quaternion.identity; Rigidbody RB = obj.physicsBody; if (RB != null) { if (obj.IsGrabbed()) { props = obj.GetRBProps(); } else { props = new bool[2] { RB.useGravity, RB.isKinematic }; } RB.useGravity = false; RB.isKinematic = true; } } this.RBprops.Add(props); this.objectsInside.Add(obj); obj.SetInteractable(!this.disableInteration); //enable / disable interactions. this.OnObjectDetected(obj); }
/// <summary> Remove an object from this dropzone and restore its original settings. </summary> /// <param name="objectIndex"></param> public void RemoveObject(int objectIndex) { //SenseGlove_Debugger.Log("The script wishes to remove " + objectIndex); if (objectIndex >= 0 && objectIndex < this.objectsInside.Count) { SenseGlove_Debugger.Log("removing " + this.objectsInside[objectIndex].name + " from the DropZone!"); SenseGlove_Grabable obj = this.objectsInside[objectIndex]; SenseGlove_Debugger.Log("RBProps.lengh = " + RBprops.Count); if (obj.GetComponent <Rigidbody>() != null && RBprops[objectIndex] != null) { //if it is currently picked up, we assign the previous properties to its grabscript, which will then apply them once it lets go. SenseGlove_Debugger.Log("It has a physicsBody"); if (obj.IsGrabbed()) { obj.SetOriginalParent(this.originalParent[objectIndex]); obj.SetRBProps(this.RBprops[objectIndex][0], this.RBprops[objectIndex][1]); } else { obj.transform.parent = this.originalParent[objectIndex]; obj.physicsBody.useGravity = this.RBprops[objectIndex][0]; obj.physicsBody.isKinematic = this.RBprops[objectIndex][1]; } } this.objectsInside.RemoveAt(objectIndex); SenseGlove_Debugger.Log("Removed it from ObjectsInside!"); this.RBprops.RemoveAt(objectIndex); this.originalParent.RemoveAt(objectIndex); obj.SetInteractable(true); //now the function can also be used to force removal of the object. this.OnObjectRemoved(obj); } }
//a collider exists in the dropZone void OnTriggerStay(Collider col) { SenseGlove_Grabable grabable = col.GetComponent <SenseGlove_Grabable>(); if (grabable && (!grabable.IsGrabbed() || this.takeFromHand)) { int index = this.ListIndex(grabable); if (index < 0) { if (this.objectsToGet.Count <= 0 || this.IsTarget(grabable)) { //SenseGlove_Debugger.Log("A new object inside the dropZone!"); this.AddObject(grabable); } } } }
//A new collider enters this dropzone void OnTriggerEnter(Collider col) { SenseGlove_Grabable grabable = col.GetComponent <SenseGlove_Grabable>(); if (grabable && (!grabable.IsGrabbed() || this.takeFromHand)) { int index = this.ListIndex(grabable); //SenseGlove_Debugger.Log("Detected an Object with listIndex of " + index); if (index < 0) //its a new object, even if it has multiple colliders. { // SenseGlove_Debugger.Log("A new object!"); if (this.objectsToGet.Count <= 0 || this.IsTarget(grabable)) //either we require multiple objects or it is one of the goal objects. { this.AddObject(grabable); } } } }
// Update is called once per frame void Update() { alleKohlenstoff = GameObject.FindGameObjectsWithTag("Kohlenstoff"); if (grabable.IsGrabbed()) { for (int j = 1; j <= alleKohlenstoff.Length; j++) { if (grabable.name != alleKohlenstoff[j - 1].name && grabable.transform.parent.name != "Molekül") { if (grabable.GetComponent <Atom>().c0.otherAtomID != alleKohlenstoff[j - 1].GetComponent <Atom>()._id&& grabable.GetComponent <Atom>().c1.otherAtomID != alleKohlenstoff[j - 1].GetComponent <Atom>()._id&& grabable.GetComponent <Atom>().c2.otherAtomID != alleKohlenstoff[j - 1].GetComponent <Atom>()._id&& grabable.GetComponent <Atom>().c3.otherAtomID != alleKohlenstoff[j - 1].GetComponent <Atom>()._id) { alreadyGrabbed = true; distance = Vector3.Distance(grabable.transform.position, alleKohlenstoff[j - 1].transform.position); if (distance <= 0.25 && (alleKohlenstoff[j - 1].transform.parent != null || alleKohlenstoff.Length <= 2) && !alleKohlenstoff[j - 1].GetComponent <Atom>().isFull) { zwischenSpeicher = alleKohlenstoff[j - 1]; alleKohlenstoff[j - 1].GetComponent <Renderer>().material.color = new Color32((byte)redCol, (byte)greenCol, (byte)blueCol, 255); if (startedFlashing == false) { startedFlashing = true; StartCoroutine(FlashObject(distance)); } break; } else if (CR_running = true && distance >= 0.25) { startedFlashing = false; StopCoroutine(FlashObject(distance)); CR_running = false; if (GameObject.Find("Molekül").GetComponent <EditMode>().editMode == true && GameObject.Find("Molekül").GetComponent <EditMode>().fixedAtom._id != alleKohlenstoff[j - 1].GetComponent <Atom>()._id) { alleKohlenstoff[j - 1].GetComponent <Renderer>().material.color = new Color32(0, 0, 0, 255); } else if (GameObject.Find("Molekül").GetComponent <EditMode>().editMode == false) { alleKohlenstoff[j - 1].GetComponent <Renderer>().material.color = new Color32(0, 0, 0, 255); } } } } } } else if (!grabable.IsGrabbed() && alreadyGrabbed == true) { alreadyGrabbed = false; StopCoroutine(FlashObject(distance)); startedFlashing = false; CR_running = false; if (zwischenSpeicher != null && distance <= 0.25 && zwischenSpeicher.GetComponent <Atom>().isFull == false) { senden.Clear(); senden.Add(zwischenSpeicher.GetComponent <Atom>()); senden.Add(grabable.GetComponent <Atom>()); grabable.transform.parent = GameObject.Find("Molekül").transform; if (zwischenSpeicher.GetComponent <Atom>() == GameObject.Find("Molekül").GetComponent <EditMode>().fixedAtom) { zwischenSpeicher.GetComponent <Renderer>().material.color = new Color32(255, 0, 0, 255); } else { zwischenSpeicher.GetComponent <Renderer>().material.color = new Color32(0, 0, 0, 255); } SendMessage("verbindungErstellen", senden); zwischenSpeicher = null; } } }