Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity UI allow a game object when touching a UI element interactive with it

Discussion in 'Scripting' started by phileday, Apr 23, 2019.

  1. phileday

    phileday

    Joined:
    Feb 8, 2014
    Posts:
    120
    Hi

    I've been looking for an answer for this for a while and I just can't find a good solution.

    What I want to do:
    I want to have a Worldspace Canvas with a UI element on it and when a gameobject is over/intersecting with a UI element it's interacting with it instead of the mouse being what interacts with it.

    Example:
    World canvas with a button on it. When the gameobject(sphere) is intersecting with it the button has a hover state as if the mouse was over it. When the submit button is pressed (normally the mouse) the button is pressed, when the gameobject is intersecting with it. The mouse does not interact with the UI.

    So there is nothing more than this. I don't want to buy a Unity3d asset or anything like that, I would like to know how to program it myself

    Is this possible? It seems something that would be straight forward but for the life of me I can't find a way to do it. I either find very complicated assets that have loads of other things in them or nothing at all.

    Anyone have any idea or can send me a link that might help?

    Any help would be very appreciated.

    Regards

    Phil
     
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    You then dont want the usual ui button to actually react but you need a script checking for 3d collision and than sets the button to the state you want.

    You can add a 2d collider to the button or a 3d collider too I guess
     
    abelguima likes this.
  3. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    270
    Phrase your question as, "I observe X. The behaviour should be Y." Because I'm pretty confused.

    Define "over/intersecting." Also "it's interacting with it."

    "World canvas with a button on it. When the gameobject(sphere) is intersecting with it. When the submit button is pressed (normally the mouse) the button is pressed, when the gameobject is intersecting with it"

    This has turned into one of those hat-wearing logic puzzles!

    Here's what I gathered:

    "I observe that a world space UGUI reacts to mouse events always. Depending on the situation, the world space UGUI should not always react to mouse events."

    Now it should be clear, your UI DNGAF why it should be turned on or off. You can attach a CanvasGroup to the highest-in-the-hierarchy element with your controls in the UGUI canvas and set its Blocks Raycasts & Interactable to true when it should react to mouse events, and false when it should not.

    Now, to detect when your "sphere" is "interacting" with an object, attach a 3D collider to the world space canvas, set the trigger flags, and use OnTrigger...

    If the situation when the UGUI should not react to mouse events is, "Whenever a 3D object appears in camera space to be overlapping the UI element," instead of using OnTrigger, use raycasts from the camera.
     
  4. phileday

    phileday

    Joined:
    Feb 8, 2014
    Posts:
    120
    Hi

    Thanks for your replies. I thought I had explained it well enough but clearly not.

    @doctorpangloss I'll look into your suggestions. I'll also explain a bit better below.

    I'll try and explain it better but be aware that I might just make it even more confusing. I'll also put it into context.

    So I'm using the UI in a VR environment. I've got a world space Canvas so that the UI is in the 3D environment and I want to interact with it with a laser pointer that I've created for the controller. The laser raycasts to the UI stopping the sphere (laser crosshair) at a collider of a plane I've positioned in the same place as the UI plane. So I want the sphere to be the thing that interacts with the UI buttons/sliders etc. I use the word "intersects" as in the 3D space the sphere cuts into the UI elements, effectively colliding with them. I want the intersection/collision to cause the interaction with the UI element. When the user presses the trigger on the controller then the UI interaction is triggers such as the pressing of a button or grabbing of a slider.

    I think I made things far to confusing because I didn't want to describe the laser pointer/VR element as I'm aware that there are unity assets out there that have already been created to interact with Unity UI's. I've bought a few of these before and I've always find them far to complicated for my purpose and seeing that I've already created the laser connection to the controller and all the other stuff the only thing I'm missing is this UI interaction with the gameobject. It seems really odd to me that the only way to interact with a UI is a mouse and cursor. I really feel I'm missing something there.

    Regarding the mouse. I don't want to interact with the UI at all with the mouse. All interaction is done through the VR environment using the laser.

    I hope that explains things a bit better.

    Thanks for your help

    Phil