Пример #1
0
    /// <summary>
    /// Raycasts from the origin for any IInteractables. If one is found it is assigned to the IInteractor.
    /// </summary>
    private void CheckForInteractables()
    {
        bool raycastHit = Physics.Raycast(origin.position, origin.forward, out RaycastHit hit, rayLength);

        if (raycastHit)
        {
            // Make sure we hit something on our interactable layer. If not, unassign any IInteractable we have currently assigned.
            if (whatIsInteractable != (whatIsInteractable | 1 << hit.transform.gameObject.layer))
            {
                if (interactor.GetInteractable() != null)
                {
                    interactor.UnassignInteractable();
                }

                return;
            }

            IInteractable interactable = hit.transform.GetComponent <IInteractable>();
            if (interactable == null)
            {
                Debug.Log("RAYCAST INTERACTION: " + hit.transform.name + " is on an interactable layer but has no IInteractable component!");
                return;
            }

            if (interactor.GetInteractable() != interactable)
            {
                interactor.SetInteractable(interactable);
            }
        }
        // If we didn't hit anything but still have an IInteractable assigned, unassign it.
        else if (interactor.GetInteractable() != null)
        {
            interactor.UnassignInteractable();
        }
    }