Interact using screen raycast

Janooba

New member
I've created an override for the Interact script to allow the user to interact with items solely by using a center-of-screen raycast. This greatly reduces complexity when working primarily with First Person view-mode. I'd like to see some form of this implemented into the base code, as it's a pretty standard operation for first person shooters.

How is this different from the standard "Raycast" option?
The raycast option works similar, but will shoot a ray from an arbitrary defined, static offset from the player's origin. Although there is an option to change direction depending on the view direction, it won't take into account crouch, or any other changes to the camera's position, and so it will never be quite lined up.

ScreenInteract.cs
C#:
using Opsive.UltimateCharacterController.Character.Abilities;
using UnityEngine;

/// <summary>
/// Interacts with another object within the scene. The object that the ability interacts with must have the Interact component added to it.
/// Uses the custom cast to detect if the player is looking directly at the object.
/// </summary>
[DefaultStartType(AbilityStartType.ButtonDown)]
[DefaultInputName("Action")]
[DefaultAbilityIndex(9)]
[DefaultAllowPositionalInput(false)]
[DefaultAllowRotationalInput(false)]
[AllowMultipleAbilityTypes]
public class ScreenInteract : Interact
{
    /// <summary>
    /// Called when the ablity is tried to be started. If false is returned then the ability will not be started.
    /// </summary>
    /// <returns>True if the ability can be started.</returns>
    public override bool CanStartAbility()
    {
        if (DetectUsingRaycast())
            return base.CanStartAbility();

        return false;
    }

    private bool DetectUsingRaycast()
    {
        // Use a raycast to detect if the character is looking at the object.
        if (Physics.Raycast(m_LookSource.Transform.position, m_LookSource.Transform.forward, out m_RaycastResult, m_CastDistance, m_DetectLayers, QueryTriggerInteraction.Collide))
        {
            var hitObject = m_RaycastResult.collider.gameObject;
            if (ValidateObject(hitObject, false))
            {
                m_DetectedObject = hitObject;
                return true;
            }
        }

        return false;
    }
}
Usage:
  1. Throw this script anywhere in your project, and add the ScreenInteract ability to your character.
  2. Add the "Screen Interact" ability to your character
  3. Set the Object Detection to Customcast
 

discofever

New member
So i'm trying this out. I've created simple "PickInteractable"


But it only writes me back "Has Interacted" every now and then not 'as i click'.
And I never see the "YAY" message.

What am i missing here ?

Thanks !


C#:
using UnityEngine;
using Opsive.UltimateCharacterController.Traits;

namespace Opsive.UltimateCharacterController.Objects.CharacterAssist
{
    public class PickInteractable : MonoBehaviour, IInteractableTarget, IInteractableMessage
    {

        public bool CanInteract(GameObject character)
        {
            return true;
        }

        public void Interact(GameObject character)
        {
            Debug.Log("Has Interacted");
        }


        public string AbilityMessage()
        {
            return "YAY Interaction completed !";
        }
    }
}
1551565172462.png


1551565006449.png
 

Janooba

New member
If you see the "Has Interacted" log, then my part of the script is working fine. It seems the issue lies with the IInteractableMessage bits. I've yet to use that, so I can't be of much help there.
 
Top