Combine Proximity Selector and Selector

Announcements, support questions, and discussion for the Dialogue System.
Luseya
Posts: 24
Joined: Mon May 29, 2023 6:02 pm

Re: Combine Proximity Selector and Selector

Post by Luseya »

Hi, it's me again :)

I'm trying to achieve the same thing that was mentioned in the original post.

In particular, I was wondering if you could explain this:
If you want to restrict mouse clicks further so they only interact if you click on the NPC, you can make a subclass of ProximitySelector and override the UseCurrentSelection() method to check if the mouse is over the NPC. If so, call the base.UseCurrentSelection() method.
Sorry, I'm not great at coding yet, so it's hard for me to follow. :(
User avatar
Tony Li
Posts: 21681
Joined: Thu Jul 18, 2013 1:27 pm

Re: Combine Proximity Selector and Selector

Post by Tony Li »

Try this script:

ProximitySelectorWithMouseOver.cs

Code: Select all

using UnityEngine;
using PixelCrushers;
using PixelCrushers.DialogueSystem;

public class ProximitySelectorWithMouseOver : ProximitySelector
{
    public LayerMask mouseCheckLayerMask = 1; // Default layer.
    public Selector.Dimension dimension = Selector.Dimension.In3D;

    protected override bool IsUseButtonDown()
    {
        return base.IsUseButtonDown() && IsMouseOverUsable();
    }

    private bool IsMouseOverUsable()
    {
        if (currentUsable == null) return false;

#if !USE_NEW_INPUT 
        // Return false if mouse is over a UI element:
        // (In new Input System, IsPointerOverGameObject returns true for all GameObjects,
        // not just UI objects, so skip this check until Input System is fixed.)
        if (UnityEngine.EventSystems.EventSystem.current != null && UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()) return false;
#endif

        var mousePosition = InputDeviceManager.GetMousePosition();
        switch (dimension)
        {
            default:
            case Selector.Dimension.In3D:
                RaycastHit hit;
                Ray ray = Camera.main.ScreenPointToRay(mousePosition);
                if (Physics.Raycast(ray, out hit, Mathf.Infinity, mouseCheckLayerMask))
                {
                    if (hit.collider.gameObject == currentUsable.gameObject) return true;
                }
                return false;
            case Selector.Dimension.In2D:
                RaycastHit2D hit2D;
                hit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(mousePosition), Vector2.zero, Mathf.Infinity, mouseCheckLayerMask);
                if (hit2D.collider != null)
                {
                    if (hit2D.collider.gameObject == currentUsable.gameObject) return true;
                }
                return false;
        }
    }
}
If your game is in 2D, change the Dimension dropdown at the bottom to In 2D.

If your Usable NPC isn't on the Default layer, add its layer to the Mouse Check Layer Mask dropdown.
Luseya
Posts: 24
Joined: Mon May 29, 2023 6:02 pm

Re: Combine Proximity Selector and Selector

Post by Luseya »

This worked perfectly! Thank you so much! I appreciate all the help :)
User avatar
Tony Li
Posts: 21681
Joined: Thu Jul 18, 2013 1:27 pm

Re: Combine Proximity Selector and Selector

Post by Tony Li »

Glad to help!
Post Reply