Search Unity

Okay, I'm going loony with figuring out how to use Raycasts with UI...

Discussion in 'UGUI & TextMesh Pro' started by MatthewJPatterson, Apr 22, 2021.

  1. MatthewJPatterson

    MatthewJPatterson

    Joined:
    Dec 30, 2016
    Posts:
    7
    I have spent way too many hours searching, reading, watching, sifting, copy and pasting, trying to solve this issue and my spirit has broke lol.

    The task I am trying to do, to me sounds like something of extreme common desire: Cast a Ray from the center of a camera (in this case a first person camera) and have is interact with a World Space Canvas exactly as a mouse does (highlights of buttons, clicking, etc).

    Here are a small selection of some of the better links I have already explored to avoid repeat solutions:

    I do not understand how to use this set up at all.

    Similar Idea here, still am having no lucking getting the actual behavior I want.

    To put the need of the behavior in perspective, I am trying to select things in the world space in the same fashion as a Real Time Strategy game but in a First Person perspective. When I select buildings and units, a World Space Canvas become enabled to issue commands. However, as you can see, I cannot figure out how to treat the center of my screen as if it was a mouse to beable to issue the commands and have the highlight visual feedback. I've been developing this for a long while now by simply unlocking my mouse from the First Person controller and clicking the commands with my actual mouse. However, I have deadlines coming up and I still can not figure out this problem. Eventually, I will be converting this to VR as a passion project beyond an assignment, and this behavior will be especially needed then with controllers.

    I'm super willing to give any needed additional information, and your time and efforts would be DEEPLY appreciated.

    Sincerely,
    Matt
     
  2. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
    If you're using CursorLockMode.Locked it won't work anymore because Unity broke it for no reason ages ago.

     
  3. MatthewJPatterson

    MatthewJPatterson

    Joined:
    Dec 30, 2016
    Posts:
    7
    I wasn't using that, but that sounds like it would have been the trick? I think its quite silly that something so seemingly mundane is so hard to get it to work in Unity. I love Unity, but dang, this is a really silly short coming as far as I can tell... unless a solution is hiding just out of reach on me - which is why I am posting here in hopes find it lol.
     
  4. MatthewJPatterson

    MatthewJPatterson

    Joined:
    Dec 30, 2016
    Posts:
    7
    Do I have to like, give every button a collider, and then reprogram the button behaviors through a Physics.Raycast? Can I even do that? Is there a method to call to make the button "highlight"? I can't find it.
     
  5. MatthewJPatterson

    MatthewJPatterson

    Joined:
    Dec 30, 2016
    Posts:
    7
    There's methods of "onPointerEnter" but I don't understand the event system and that sounds like its regarding the mouse - not a raycast because of the consistent "pointer" usage? I guess what I'm going to do is literally reprogram a button using .color for highlights and .onClick with coliders and Physics raycast. I hope there's a bool tick mark on buttons sometime soon to natively support raycasts as expected :/. I would think this would be a thing that would be implemented simultaneously with the implementation of World Space Canvas's. :confused:lol
     
  6. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
    Just replace StandaloneInputModule (on the EventSystem gameobject) with this:
    https://gist.github.com/EmmaEwert/fad38a248f62e1c7267daa4e778468b1

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class FirstPersonInputModule : StandaloneInputModule
    5. {
    6.     protected override MouseState GetMousePointerEventData(int id)
    7.     {
    8.         var lockState = Cursor.lockState;
    9.         Cursor.lockState = CursorLockMode.None;
    10.         var mouseState = base.GetMousePointerEventData(id);
    11.         Cursor.lockState = lockState;
    12.         return mouseState;
    13.     }
    14.  
    15.     protected override void ProcessMove(PointerEventData pointerEvent)
    16.     {
    17.         var lockState = Cursor.lockState;
    18.         Cursor.lockState = CursorLockMode.None;
    19.         base.ProcessMove(pointerEvent);
    20.         Cursor.lockState = lockState;
    21.     }
    22.  
    23.     protected override void ProcessDrag(PointerEventData pointerEvent)
    24.     {
    25.         var lockState = Cursor.lockState;
    26.         Cursor.lockState = CursorLockMode.None;
    27.         base.ProcessDrag(pointerEvent);
    28.         Cursor.lockState = lockState;
    29.     }
    30. }