private void CheckTriggerExit(GameObject other) { Usable usable = other.GetComponent <Usable>(); if (usable != null) { if (usablesInRange.Contains(usable)) { usablesInRange.Remove(usable); } if (currentUsable == usable) { if (DeselectedUsableObject != null) { DeselectedUsableObject(usable); } currentUsable = null; if (usablesInRange.Count > 0) { currentUsable = usablesInRange[0]; if (SelectedUsableObject != null) { SelectedUsableObject(currentUsable); } } } } }
/// <summary> /// Runs a raycast to see what's under the selection point. Updates the selection and /// calls the selection delegates if the selection has changed. If the player hits the /// use button, sends an OnUse message to the selection. /// </summary> void Update() { // Exit if disabled or paused: if (!enabled || (Time.timeScale <= 0)) { return; } // Exit if there's no camera: if (Camera.main == null) { return; } // Cast a ray and see what we hit: Ray ray = Camera.main.ScreenPointToRay(GetSelectionPoint()); RaycastHit hit; if (Physics.Raycast(ray, out hit, maxSelectionDistance, layerMask)) { distance = (distanceFrom == DistanceFrom.Camera) ? hit.distance : Vector3.Distance(gameObject.transform.position, hit.collider.transform.position); if (selection != hit.collider.gameObject) { Usable hitUsable = hit.collider.gameObject.GetComponent <Usable>(); if (hitUsable != null) { usable = hitUsable; selection = hit.collider.gameObject; if (SelectedUsableObject != null) { SelectedUsableObject(usable); } } else { DeselectTarget(); } } } else { DeselectTarget(); } // If the player presses the use key/button, send the OnUse message: if (IsUseButtonDown() && (usable != null) && (distance <= usable.maxUseDistance)) { if (broadcastToChildren) { usable.gameObject.BroadcastMessage("OnUse", this.transform, SendMessageOptions.DontRequireReceiver); } else { usable.gameObject.SendMessage("OnUse", this.transform, SendMessageOptions.DontRequireReceiver); } } }
private void DeselectTarget() { if ((usable != null) && (DeselectedUsableObject != null)) { DeselectedUsableObject(usable); } usable = null; selection = null; }
private void CheckTriggerEnter(GameObject other) { Usable usable = other.GetComponent <Usable>(); if (usable != null) { currentUsable = usable; if (!usablesInRange.Contains(usable)) { usablesInRange.Add(usable); } if (SelectedUsableObject != null) { SelectedUsableObject(usable); } } }