// Remove item from the hast set as it leaves the collider private void OnTriggerExit(Collider collider) { Interactable_Item colliderItem = collider.GetComponent <Interactable_Item>(); pickup = null; if (colliderItem) { objectsHoveringOver.Remove(colliderItem); } }
/* ------------------------ Triggers ---------------------------*/ // Adds gameObject which collide with the controller into the hashSet private void OnTriggerEnter(Collider collider) { // Add to hash table Interactable_Item colliderItem = collider.GetComponent <Interactable_Item>(); pickup = collider.gameObject; if (colliderItem) { objectsHoveringOver.Add(colliderItem); } }
// Update is called once per frame void Update() { if (controller == null) { Debug.Log("Controller not initialized"); return; } if (controller.GetPressDown(gripButton) && pickup != null) { float minDistance = float.MaxValue; float distance; // Find the gameObject which is closest to the controlelr foreach (Interactable_Item item in objectsHoveringOver) { distance = (item.transform.position - transform.position).sqrMagnitude; if (distance < minDistance) { minDistance = distance; closestItem = item; } } interactingItem = closestItem; if (interactingItem) { interactingItem.GetComponent <Rigidbody>().drag = 0f; if (interactingItem.IsInteracting()) { interactingItem.EndInteraction(this); isInteracting = false; } interactingItem.BeginInteraction(this); isInteracting = true; } } // stop the interaction if grip button is released if (controller.GetPressUp(gripButton) && interactingItem != null) { interactingItem.EndInteraction(this); isInteracting = false; interactingItem = null; } }