Esempio n. 1
0
        /// <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>
        protected void Update()
        {
            // Exit if disabled or paused:
            if (!enabled || (Time.timeScale <= 0))
            {
                return;
            }

            // Exit if there's no camera:
            if (UnityEngine.Camera.main == null)
            {
                return;
            }

            // Exit if using mouse selection and is over a UI element:
            if ((selectAt == SelectAt.MousePosition) && (UnityEngine.EventSystems.EventSystem.current != null) && UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
            {
                return;
            }

            // Raycast 2D or 3D:
            switch (runRaycasts)
            {
            case Dimension.In2D:
                Run2DRaycast();
                break;

            default:
            case Dimension.In3D:
                Run3DRaycast();
                break;
            }

            // If the player presses the use key/button on a target:
            if (IsUseButtonDown() && (usable != null))
            {
                clickedDownOn = null;
                if (distance <= usable.maxUseDistance)
                {
                    // If within range, send the OnUse message:
                    var fromTransform = (actorTransform != null) ? actorTransform : this.transform;
                    if (broadcastToChildren)
                    {
                        usable.gameObject.BroadcastMessage("OnUse", fromTransform, SendMessageOptions.DontRequireReceiver);
                    }
                    else
                    {
                        usable.gameObject.SendMessage("OnUse", fromTransform, SendMessageOptions.DontRequireReceiver);
                    }
                }
                else
                {
                    // Otherwise report too far if configured to do so:
                    if (!string.IsNullOrEmpty(tooFarMessage))
                    {
                        DialogueManager.ShowAlert(tooFarMessage);
                    }
                    tooFarEvent.Invoke();
                }
            }
        }