hi all
I’m trying to detect an image object inside a canvas containing multiple images , I’m
use Raycast2dhit to detect the name of the image when clicking it, the thing is I keep getting null value
when clicking the images , all the images in the canvas has colliders, I have a script attached to scene manager that detect object that is hit by the ray
the code:
public void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if (hit.collider != null)
{
Debug.Log(hit.collider.gameObject.name);
}
}
}
Are you raycasting into a Canvas hierarchy, in which case you do NOT use colliders (see the old docs for GraphicRaycaster; the new docs are nearly useless)? You would be testing for the Raycast Target property for this.
Or are you raycasting at 2D objects, in which case you probably want to use the OverLap series of methods, perhaps OverlapPoint.
Either way…
How to report your problem productively in the Unity3D forums:
This is the bare minimum of information to report:
what you want
what you tried
what you expected to happen
what actually happened, log output, variable values, and especially any errors you see
links to documentation you used to cross-check your work (CRITICAL!!!)
The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?
ok, so I tried sitting colliders for the images and tried to use raycaster2d to capture the image objects, I also tried removing the collider from the images and used graphics ray caster with the help of unity docs Unity - Scripting API: UI.GraphicRaycaster.Raycast
i tryed removing and recreating the image objects from scratch also didn’t work?
Whilst raycast of zero length works, you should really be using OverlapPoint. There’s nothing wrong with the physics queries so it’s impossible to verify that the world position you’re passing does indeed have a collider there; only you can do that.
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hit = Physics2D.GetRayIntersection(ray);
Collider2D collider = Physics2D.OverlapPoint(hit.collider.offset);
Debug.Log("colider=" + collider);
if (hit.collider != null)
{
Debug.Log("hit" + hit.collider.gameObject.name);
}
}
}
}
and when i run the program and click the image with collider what is getting captured is the canvas , so the question is how to make the ray ignore the canvas and keep going to the image object?
The physics system isn’t 3D. It doesn’t know anything about UI. Those queries do exactly what they say on the tin. 2D physics doesn’t give you render order or topmost. GetRayIntersection will give you the 2D colliders sorted by Transform Z though.
Your code above is very odd. You use OverlapPoint to detect the collider. Why you get a single hit from a ray then go and do an overlap point I don’t know.
OverlapPoint will give you a single or multiple colliders depending on which overload you use.