Problem with UI GraphicRaycasting
Hi everyone!
I have a problem with understanding, how ui.element.raycast() works.
Here is an example. I have a canvas, which have a panel as child. Panel have have two children, both are images.
I move one image by mouse (by Input.GetAxis()). When I click mosue button, next code start working:
void Check(Vector3 position)
{
Camera camera = GameObject.Find("Main Camera").GetComponent<Camera>();
Vector2 screenPos = camera.WorldToScreenPoint(position);
bool hit = Img.Raycast(screenPos, camera);
if (hit)
Debug.Log("Hit");
else
Debug.Log("Miss");
}
Where "position" is position of moveble image in worldSpace coordinates, and Img is static second image.
Even when moveble image far away from static, hit is always equal true.
Can someone explayne me why?
Hi @Berdoffy - Your question is really vague. You say you have a problem understanding how UI raycasting works. I have no idea, what you mean by "Even when moveble image far away from static, hit is always equal true." - UI raycasting has nothing to do with moving objects. Without looking at your code - what are you actually trying to do?
Hi, @eses - I figure out, what was wrong.
What i was trying to do is to call function Image.Raycast(Vector2 position, Camera camera). For position i took transform.position of ui object - image, which can change position via moving. Another object also ui image, but static (does't move), from which I called Raycast();
Problem was that even if "position" was not in RectTransform of static image, Raycast() always returned true.
But when I've tryed another sprite for static image, with transparent gapes bitween sprite edges (from all 4 sides), problem was gone, and Raycast returned true only, when "position" parametr was inside static image's sprite.
Sorry for my english and vague narration. And thank you for your attempt to help!
Answer by Berdoffy · Aug 29, 2018 at 02:55 PM
I've figure out what was a problem.
Image should have tranparent clearence on edges. Otherwise it is virtualy duplicates somehow (could be wrong, but this how i see it).
Your answer
Follow this Question
Related Questions
I am trying to close the GUI if its already active and the user clicks 2 Answers
UI panel without Image component as Raycast target? It is possible? 5 Answers
List becomes empty when accessed in another class 0 Answers
How to make an Inventory Hotbar (C#) 0 Answers
Instantiated in IF statement - Else Reference object 1 Answer