- Home /
Detect when UI element in canvas overlaps with moving game object and then make element follow object
Hello,
I'm using VR in Unity and have 3D game objects moving behind a UI canvas on which there's a raw image. The canvas and image render at a fixed location above everything else ("render mode" is "screen space" on a separate camera and layer). Basically it's like a HUD for VR. I would like, though, to detect when the raw image on that HUD overlaps with a specific game object that moves behind it and when that happens the image should lock and "follow" the object on the canvas plane until the user presses a key.
My idea to start implementing this was to cast a ray from the target game object to the camera and check whether it hits the image in the canvas, but I wasn't successful.
    int uiLayer = LayerMask.NameToLayer("UI");
     Ray raycast = new Ray(gameobject.transform.position, Camera.main.transform.position- gameobject.transform.position);
     RaycastHit hit;
     bool bHit = Physics.Raycast(raycast, out hit, Mathf.Infinity, uiLayer);
     if (bHit)
     {
         var hitObj = hit.collider.gameObject;
         if (hitObj == hudImage)
             Debug.Log("HUD!");
     }
I read in other answers that Physics.Raycast doesn't collide with 2D UI elements (even though I checked "raycast target" in the raw image script), but I'm not sure then how to achieve what I describe above.
Any help/code appreciated.
Your answer
 
 
             Follow this Question
Related Questions
Help with Raycast on Render Texture 0 Answers
Canvas Disabled but UI elements still block Raycasts 4 Answers
Interacting with images on the canvas 0 Answers
UI Buttons visible but not clickable when using two canvases. 3 Answers
How to get PointerEventDatas[]? 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                