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
 
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
 
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.
 
Sorry for bringing all these threads back from the dead but has any of this been included it the package?

We're working on a FPS survival type game and this functionality is integral.

I've been in contact with the original poster of this thread and they gave me the provided script that seems to allow some type of interaction (the object is being destoryed when you click on it) but the ItemPickup.TriggerEnter event the KeyItemPickup isn't register by the ItemPickup script.

Is there anything in the package that allows for a button press to interact with pickup items?

C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using Opsive.UltimateCharacterController.Traits;

public class KeyItemPickup : MonoBehaviour, IInteractableTarget
{
    public bool canPickup = true;

    public UnityEvent onPickup;

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

    public void Interact(GameObject character)
    {
        Debug.Log("Picked up!");
        gameObject.SetActive(false);
        onPickup?.Invoke();
    }
}
 
Justin,

I know this is an old thread and I do see a lot of other Interact abilities as well as object detections via ray casting;- so was wondering if this was integrated into existing core or a separate ability?
 
You can do a regular raycast and use the look position/direction which will then use the center of the screen in first person mode.
 
Top