protected virtual void DoInteraction()
        {
            // Do the raycast.
            if (Physics.Raycast(
                    cameraHead.position,
                    cameraHead.forward,
                    out interactableHit,
                    interactionRange,
                    interactionLayer,
                    triggerInteraction))
            {
                // If there's no hit transform, stop here.
                if (interactableHit.collider == null)
                {
                    return;
                }

                // If there's no current hit or the hits doesn't match, update it and
                // the player need to check for a interactable again.
                if (currentHit == null || currentHit != interactableHit.collider)
                {
                    currentHit = interactableHit.collider;
                    haveCheckedInteractable = false;
                }

                // If the player hasn't checked for an interactable, do so ONCE.
                // We don't want to call GetComponent every frame, you know!
                if (!haveCheckedInteractable)
                {
                    // Prefer interactables on the collider itself, but if the collider doesn't
                    // have one, then look on the rigidbody.
                    CurrentHitInteractable = interactableHit.collider.GetComponent <IGoldPlayerInteractable>();
                    if (CurrentHitInteractable == null && interactableHit.rigidbody != null)
                    {
                        interactableHit.rigidbody.GetComponent <IGoldPlayerInteractable>();
                    }

                    haveCheckedInteractable = true;
                }

                // Set Can Interact depending on if the player has a interactable object
                // and it can be interacted with.
                CanInteract = CurrentHitInteractable != null && CurrentHitInteractable.CanInteract;

                // If the player presses the interact key and it can react, call interact.
                if (GetButtonDown(interactInput) && CanInteract)
                {
                    CurrentHitInteractable.Interact();
                }
            }
            else
            {
                // There's nothing to interact with.
                CanInteract            = false;
                CurrentHitInteractable = null;
                currentHit             = null;
            }
        }
Beispiel #2
0
        internal void HitInteraction(RaycastHit hit)
        {
            // If there's no hit transform, stop here.
            if (hit.collider == null)
            {
                return;
            }

            // If there's no current hit or the hits doesn't match, update it and
            // the player need to check for a interactable again.
            if (currentHit == null || currentHit != hit.collider)
            {
                currentHit = hit.collider;
                haveCheckedInteractable = false;
            }

            // If the player hasn't checked for an interactable, do so ONCE.
            // We don't want to call GetComponent every frame, you know!
            if (!haveCheckedInteractable)
            {
                // Prefer interactables on the collider itself, but if the collider doesn't
                // have one, then look on the rigidbody.
                CurrentHitInteractable = hit.collider.GetComponent <IGoldPlayerInteractable>();
                if (CurrentHitInteractable == null && hit.rigidbody != null)
                {
                    CurrentHitInteractable = hit.rigidbody.GetComponent <IGoldPlayerInteractable>();
                }

                haveCheckedInteractable = true;
            }

            // Set Can Interact depending on if the player has a interactable object
            // and it can be interacted with.
            CanInteract = CurrentHitInteractable != null && CurrentHitInteractable.CanInteract;

            // If the player presses the interact key and it can react, call interact.
            if (CanInteract && GetButtonDown(interactHash))
            {
                CurrentHitInteractable.Interact();
            }
        }
Beispiel #3
0
 protected virtual void DoInteraction()
 {
     // Do the raycast.
     if (Physics.Raycast(
             cameraHead.position,
             cameraHead.forward,
             out interactableHit,
             interactionRange,
             interactionLayer,
             triggerInteraction))
     {
         HitInteraction(interactableHit);
     }
     else
     {
         // There's nothing to interact with.
         CanInteract            = false;
         CurrentHitInteractable = null;
         currentHit             = null;
     }
 }