Exemplo n.º 1
0
    void CheckForInteractable()
    {
        Ray        _ray = new Ray(m_cam.transform.position, m_cam.transform.forward);
        RaycastHit _hitInfo;
        bool       _hitSomething = Physics.SphereCast(_ray, raySphereRadius, out _hitInfo, rayDistance, interactableLayer);

        // debug
        Debug.DrawRay(_ray.origin, _ray.direction, _hitSomething?Color.green:Color.red);
        // -----

        if (_hitSomething)
        {
            InteractableBase _interactable = _hitInfo.transform.GetComponent <InteractableBase>();
            if (_interactable != null && _interactable.IsInteractable)
            {
                if (interactionData.IsEmpty() || (!interactionData.IsSameInteractable(_interactable)))
                {
                    interactionData.Interactable = _interactable;
                    uiPanel.SetToolTip(_interactable.TooltipMessage);
                }
            }
        }
        else
        {
            uiPanel.ResetUI();
            interactionData.ResetData();
        }
    }
Exemplo n.º 2
0
    void CheckForInteractable()
    {
        //Ray to the direction : Front of player.
        m_ray = new Ray(rayPositionOffset.position, gameObject.transform.forward);

        RaycastHit _hitInfo;

        // m_hitSomething = Physics.SphereCast(m_ray, raySphereRadius, out _hitInfo, rayDistance, interactableLayer);
        m_hitSomething = Physics.Raycast(m_ray, out _hitInfo, rayDistance, interactableLayer);


        if (m_hitSomething)
        {
            InteractableBase _interactable = _hitInfo.transform.GetComponent <InteractableBase>();

            if (_interactable == null)
            {
                _interactable = _hitInfo.transform.GetComponentInChildren <InteractableBase>();
            }

            if (_interactable != null)
            {
                //Fill Interaction Data
                if (interactionData.IsEmpty())
                {
                    interactionData.Interactable = _interactable;
                }
                else
                {
                    if (!interactionData.IsSameInteractable(_interactable))
                    {
                        interactionData.Interactable.StopParticles();
                        interactionData.Interactable = _interactable;
                    }
                }

                interactionData.Interactable.PlayParticles();
            }
        }
        else
        {
            if (interactionData.Interactable)
            {
                interactionData.Interactable.StopParticles();
            }

            //Reset interaction Data
            interactionData.ResetData();
        }
    }
    private void CheckDetection()
    {
        /*
         * 3 Transitions/Cases here:
         * 1. No Object -> New Object
         * 2. Current Object -> New Object
         * 3. Current Object -> No Object
         */

        InteractionData interactionData = playerContext.InteractionData;

        // Case 1
        if (interactionData.LastInteractable == null && !interactionData.IsEmpty())
        {
            interactionData.CurrentInteractable.OnDetectionEnter();
            interactionData.LastInteractable = interactionData.CurrentInteractable;
            return;
        }

        // Case 2
        if (interactionData.LastInteractable != null && interactionData.CurrentInteractable != null)
        {
            if (!interactionData.IsSame(interactionData.LastInteractable))
            {
                interactionData.LastInteractable.OnDetectionExit();
                interactionData.CurrentInteractable.OnDetectionEnter();
                interactionData.LastInteractable = interactionData.CurrentInteractable;
                return;
            }
        }

        // Case 3
        if (interactionData.LastInteractable != null && interactionData.IsEmpty())
        {
            interactionData.LastInteractable.OnDetectionExit();
            interactionData.LastInteractable = null;
            return;
        }
    }
    private void CheckForInteractable()
    {
        Ray        _ray = new Ray(p_cam.transform.position, p_cam.transform.forward);
        RaycastHit hitInfo;

        //look straight ahead (with some wiggle room) for an interactable object

        bool hit = Physics.SphereCast(_ray, rayRadius, out hitInfo, rayDistance);//, interactableLayer);

        if (hit)
        {
            IInteractable _interactable = hitInfo.transform.GetComponent <IInteractable>();

            if (_interactable != null && _interactable.IsInteractable)
            {
                if (interactionData.IsEmpty() || !interactionData.IsSameObj(_interactable))
                {
                    //If there is a hit and the object hit is not already in controller, update the item in the controller
                    interactionData.Interactable = _interactable;
                    hoverUIController.SetHoverMessage(interactionData.Interactable.HoverMessage);
                }
                else
                {
                    //Ensures that hover message is up to date
                    hoverUIController.SetHoverMessage(interactionData.Interactable.HoverMessage);
                }
            }
        }
        else
        {
            //if there is no hit, reset
            interactionData.Reset();
            hoverUIController.ResetUI();
        }

        Debug.DrawRay(_ray.origin, _ray.direction, hit ? Color.green : Color.red);
    }
    private void CheckInput()
    {
        InteractionData interactionData = playerContext.InteractionData;
        Interactable    interactable    = interactionData.CurrentInteractable;

        if (interactionData.IsEmpty())
        {
            return;
        }

        if (_interactionInput.InteractPush)
        {
            _interactionInput.InteractPush = false;
            interactionData.InteractingNow = true;
            _interactionTimer.Restart();
        }

        if (_interactionInput.InteractRelease)
        {
            _interactionInput.InteractRelease = false;
            interactionData.InteractingNow    = false;
            _interactionTimer.Reset();
        }

        if (interactionData.InteractingNow)
        {
            if (!interactable.IsInteractable)
            {
                return;
            }

            if (interactable.HoldToInteract)
            {
                if (_interactionTimer.Get() > interactable.HoldDuration)
                {
                    _interactionTimer.Reset();
                    interactable.OnInteraction(playerContext);
                    interactionData.InteractingNow = false;
                }
            }
            else
            {
                _interactionTimer.Reset();
                interactable.OnInteraction(playerContext);
                interactionData.InteractingNow = false;
            }
        }
    }