Comments and answers for "Raycast on a 2D Collider"http://answers.unity.com/questions/596792/raycast-on-a-2d-collider.html
The latest comments and answers for the question "Raycast on a 2D Collider"Comment by sandolkakos on sandolkakos's answerhttp://answers.unity.com/comments/1545622/view.html
I didn't know about this. Thank you so much, @It3ration . Now I can get Collider2D from Cameras with Perspective projection.Thu, 23 Aug 2018 19:59:33 GMTsandolkakosComment by Utoxin on Utoxin's answerhttp://answers.unity.com/comments/1475689/view.html
This is the most useful answer here. It allows you to use 3D raycasts from the camera to check for 2D colliders. Very helpful!Fri, 02 Mar 2018 15:09:07 GMTUtoxinComment by ProbePLayer on ProbePLayer's answerhttp://answers.unity.com/comments/1464664/view.html
Yes this answer is clearly the best here.Mon, 05 Feb 2018 22:03:57 GMTProbePLayerComment by ProbePLayer on ProbePLayer's answerhttp://answers.unity.com/comments/1464662/view.html
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 :)Mon, 05 Feb 2018 22:02:36 GMTProbePLayerComment by M0no on M0no's commenthttp://answers.unity.com/comments/1225549/view.html
please edit your answer above to include this infoFri, 05 Aug 2016 14:51:15 GMTM0noComment by cmos on cmos's commenthttp://answers.unity.com/comments/1155507/view.html
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);
}Tue, 15 Mar 2016 20:28:35 GMTcmosComment by cmos on cmos's answerhttp://answers.unity.com/comments/1155506/view.html
This is the only solution that worked for me in this thread.Tue, 15 Mar 2016 20:26:08 GMTcmosAnswer by sushanta1991http://answers.unity.com/answers/866505/view.html
You can try this solution given in this [link][1]
[1]: http://sushanta1991.blogspot.in/2015/01/raycast-in-unity2d-using-mouse-position.htmlThu, 01 Jan 2015 11:46:27 GMTsushanta1991Comment by luniac on luniac's answerhttp://answers.unity.com/comments/825389/view.html
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, Mathf.Infinity);Thu, 06 Nov 2014 15:05:59 GMTluniacAnswer by It3rationhttp://answers.unity.com/answers/774399/view.html
Use the following to cast 3D rays against 2D colliders:
Physics2D.GetRayIntersection(...)
Physics2D.GetRayIntersectionAll(...)Tue, 19 Aug 2014 21:36:59 GMTIt3rationAnswer by citizen_rafiqhttp://answers.unity.com/answers/694362/view.html
private void OnClickGUIWidget(Vector3 position)
{
Ray ray = Camera.main.ScreenPointToRay(position);
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);
if (hit != null)
{
if (hit.collider !=null)
{
Button _btn = hit.transform.gameObject.GetComponent<Button> ();
if (_btn != null )
{
if (_btn.name == GlobalVar.button_name_exit)
{
Application.Quit();
}
if (_btn.name == GlobalVar.button_name_restart)
{
Application.LoadLevel(0);
}
}
}
}
}Fri, 25 Apr 2014 07:39:11 GMTcitizen_rafiqComment by AntFitch on AntFitch's answerhttp://answers.unity.com/comments/640723/view.html
This is a nice snippet if Physics2D.OverlapPointAll() is used: http://forum.unity3d.com/threads/211708-Unity-2D-Raycast-from-mouse-to-screenSun, 16 Feb 2014 00:40:13 GMTAntFitchComment by robertbu on robertbu's answerhttp://answers.unity.com/comments/640632/view.html
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.Sat, 15 Feb 2014 22:24:58 GMTrobertbuComment by AntFitch on AntFitch's answerhttp://answers.unity.com/comments/640628/view.html
To clarify:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);Sat, 15 Feb 2014 22:20:10 GMTAntFitchComment by bsbimalbose on bsbimalbose's answerhttp://answers.unity.com/comments/597330/view.html
Thanks I added 3D colliders as childern to the Sprite Objects and it works fineSun, 15 Dec 2013 15:08:47 GMTbsbimalboseAnswer by robertbuhttp://answers.unity.com/answers/596911/view.html
As you have found, 3D Raycasting (Physics.Raycast()) on a 2D colliders does not work. You have a couple of choices. First [Monobehaviour.OnMouse*][1] 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.
[1]: http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.htmlSat, 14 Dec 2013 20:12:06 GMTrobertbuAnswer by MarcosLChttp://answers.unity.com/answers/596818/view.html
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.Sat, 14 Dec 2013 18:49:25 GMTMarcosLCAnswer by _MGB_http://answers.unity.com/answers/596821/view.html
If you're using the new 2d stuff you'll need to use the [Physics2D functions][1].
[1]: http://docs.unity3d.com/Documentation/ScriptReference/Physics2D.htmlSat, 14 Dec 2013 17:55:54 GMT_MGB_