- Home /
Raycast on a 2D Collider
I want to raycast from the camera to the mouse location for my @D Game to identify which Object was clicked.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray,out hit))
{
isHit = false;
Destroy(GameObject.Find(hit.collider.gameObject.name));
}
Raycasting on 2D Colliders doesn't seem to work.
Answer by robertbu · Dec 14, 2013 at 08:12 PM
As you have found, 3D Raycasting (Physics.Raycast()) on a 2D colliders does not work. You have a couple of choices. First Monobehaviour.OnMouse* functions do work, so you can put a script directly on the object. If you want to Raycast(), then an alternate solution is to put a 3D collider on an empty child game object and size the collider as appropriate to your sprite. You can use the Transform.parent to access the sprite.
Thanks I added 3D colliders as childern to the Sprite Objects and it works fine
Note my answer here is old and while workable, is not the best answer. The Physics2D.Raycast() using a Vector2.zero for direction works fine. In addition, while I've not seen it in an answer, Physics2D.OverlapPoint() might even be cleaner.
This is a nice snippet if Physics2D.OverlapPointAll() is used: http://forum.unity3d.com/threads/211708-Unity-2D-Raycast-from-mouse-to-screen
Is overlap point possibly cleaner than this? as long as ur looking for the first touch that is.
var worldTouch = Camera.main.ScreenToWorldPoint(touch.Position);
var hit : RaycastHit2D;
hit = Physics2D.Raycast (Vector2(worldTouch.x,worldTouch.y),Vector2.zero, $$anonymous$$athf.Infinity);
There is a third solution which is by far the most accurate for the question, and it is described in the @sushanta1991 's answer. Namely, you use Physics2D.Raycast function but with a twist. You raycast from the mouse position to the mouse position, which gives you any object under the mouse position :)
Answer by MarcosLC · Dec 14, 2013 at 06:49 PM
For 2D Colliders you need use Physics2D and RaycastHit2D instead of Physics.
void Update(){
RaycastHit2D hit = Physics2D.Raycast(cameraPosition, mousePosition, distance (optional));
if(hit != null && hit.collider != null){
isHit = false;
Destroy(GameObject.Find(hit.collider.gameObject.name));
}
}
Use it with a Vector2 mousePosition or if your game is 3D use 3D colliders instead of 2D.
To clarify:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);
Answer by sushanta1991 · Jan 01, 2015 at 11:46 AM
You can try this solution given in this link
This is the only solution that worked for me in this thread.
Vector3 mousePos = Input.mousePosition;
mousePos.z = 10;
Vector3 screenPos = Camera.main.ScreenToWorldPoint(mousePos);
RaycastHit2D hit = Physics2D.Raycast(screenPos,Vector2.zero);
if(hit)
{
print (hit.collider.name);
}
Answer by It3ration · Aug 19, 2014 at 09:36 PM
Use the following to cast 3D rays against 2D colliders:
Physics2D.GetRayIntersection(...)
Physics2D.GetRayIntersectionAll(...)
This is the most useful answer here. It allows you to use 3D raycasts from the camera to check for 2D colliders. Very helpful!
I didn't know about this. Thank you so much, @It3ration . Now I can get Collider2D from Cameras with Perspective projection.
I did the login only to upvote this answer, thanks dude, this solved my problem, upvote this guy
I love you, man. I've been using Unity for 5 years, that's the first time I hear about these methods.
Answer by _MGB_ · Dec 14, 2013 at 05:55 PM
If you're using the new 2d stuff you'll need to use the Physics2D functions.