3

Hi2,

I have (a very simple, i think) problem. How do I detect & interact with the Ui Element that is currently being "raycasted" ?

The figure below shows what i want to achieve: enter image description here

I have this code from Unity Documentation

void FixedUpdate()
{
    int layerMask = 1 << 8;

    // This would cast rays only against colliders in layer 8.
    // But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
    layerMask = ~layerMask;

    RaycastHit hit;
    // Does the ray intersect any objects excluding the player layer
    if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity, layerMask))
    {
        Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
        Debug.Log("Did Hit");
    }
    else
    {
        Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 1000, Color.white);
        Debug.Log("Did not Hit");
    }
}

but it just allow me to raycast a 3D object(with collider).

I heard about graphic raycaster and raycast all but not sure how to use it.
If possible, I don't want to attach extra script or extra event to the UI element(cause I have a lot of UI Element)

Thank you very much in advance for your input! :D

Hikari
  • 458
  • 1
  • 11
  • 27
  • So you want to move the raycast and something should happen in UI? – kiner_shah Nov 16 '21 at 09:18
  • yes..., I want to move the Sphere(with Raycast) and that raycast should "click" the button or "Scroll" the Scroll View... :) – Hikari Nov 16 '21 at 09:19
  • Not sure how to do in Unity, but the 3D raycast vector needs to be projected on 2D UI plane and it needs to be checked if the raycast focus is in the button/scrollbar area - something like that. – kiner_shah Nov 16 '21 at 09:22
  • It's a bit old but maybe this helps you: [Using Raycast instead of Gaze Pointer](https://stackoverflow.com/questions/56609334/using-raycast-instead-of-gaze-pointer) – derHugo Nov 16 '21 at 10:28
  • Are all your UI items within the same parent `Canvas` ? – derHugo Nov 16 '21 at 10:34
  • yes.., all ui is within one world Canvas – Hikari Nov 16 '21 at 11:15
  • Is it possible without adding box collider, or extra script in the ui. ? Cause if I am not mistaken, your link do add extra component. – Hikari Nov 16 '21 at 11:25

2 Answers2

1
var eventData = new PointerEventData(EventSystem.current);
  eventData.position = Input.mousePosition;
  var results = new List<RaycastResult>();
  EventSystem.current.RaycastAll(eventData, results);
  if(results.Where(r => r.gameObject.layer == 6).Count() > 0) //6 being my UILayer
  {
    Debug.Log(results[0].gameObject.name); //The UI Element
  }

Hope this helps =)

Make sure your UI elements have a 2D collider that's not a trigger.

-1

I think you need this : EventSystem.current.currentSelectedGameObject

More About this here:

https://docs.unity3d.com/2018.1/Documentation/ScriptReference/EventSystems.EventSystem-currentSelectedGameObject.html

  • hmm.. I am not sure about the EventSystem.current.currentSelectedGameObject, cause it doesn't simply return the object. Moreover that function (as far as i know) only returns the ui that is being hovered., not the one being raycasted. Thanks for your reply though – Hikari Nov 17 '21 at 08:42
  • if you want to set the game object on raycast this can help:https://docs.unity3d.com/2018.1/Documentation/ScriptReference/EventSystems.EventSystem.SetSelectedGameObject.html – Moazan Amjad Nov 18 '21 at 04:51