- Home /
Detect touched 2d object in Unity3d 4.3
Hi, How to detect 2D object in unity 4.3, I have tried code which following mentioned but it gets crash because I think this is for 3D objects. Could you please help me on that how can I detect 2D Object(name or tag) if user tap on object. I have all 2D Objects. Here is my code what I am doing to detect name of objects.
if (iPhoneInput.touchCount == 1 && iPhoneInput.touches[0].phase == iPhoneTouchPhase.Began)
{
Ray r = camera.ScreenPointToRay(new Vector3(Input.touches[0].position.x, Input.touches[0].position.y, 0f));
RaycastHit hit0;
switch (hit0.transform.gameObject.name)
{
case "char_enemy_alienSlug-dead":
hit0.transform.gameObject.SendMessage("BubbleTouched", SendMessageOptions.DontRequireReceiver);
break;
case "char_enemy_alienShip":
hit0.transform.gameObject.SendMessage("Touched", SendMessageOptions.DontRequireReceiver);
break;
}
}
}
Kindly help me on this, I used Ray2D and RaycastHit2D but it also didn't work out. Kindly share with me bit code to detect object name or tag which is touched. Thanks in advance.
Answer by robertbu · Dec 04, 2013 at 04:14 PM
Whether you are 3D or 2D, you have to actually cast the ray using some form of raycast either Physics.Raycast() or Physics.Raycast2D(). Just creating a ray doesn't initialize the RaycastHit structure. For this use, you want the 3D version (RaycastHit/Physics.Raycast()). And the raycast is not guarenteed to hit anything, so you need to check. See Raycast examples:
http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
Thanks robertbu, I tried this code { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, 100)) print("Hit something"); } in Update function and hit 2D object but this code not being call, I have added rigid body and collider to these objects. I don't know where I am doing wrong, this may be because I have to use 2D code like use Ray2D ins$$anonymous$$d of Ray, $$anonymous$$indly help me on that?
Input.mousePosition is Vector3 type, what is alternate function for finger single touch which has Vector3 type because ScreenPointToRay takes Vector2 type. Thanks again.
@robertbu, Did u get my problem for 2D in Unity4.3. I am still stuck over there.
I've not played with the new 2D stuff much, so I just ran some tests. What I found in my limited testing:
You cannot add a 2D and 3D collider to the same object
Raycast does not detect 2D colliders
On$$anonymous$$ouseDown() Does detect 2D colliders
A $$anonymous$$esh collider does not appear to work with a sprite
Any time you raycast from the screen you have a 3D collider. You have several choices:
You can attach a 3D collider like a box collider to a child object and then use the hit on that child.
You can use On$$anonymous$$ouseDown() for processing or to set a flag of some sort.
You can use the Physics2D functions to make the detection. You would convert the position into a world point then use the xy in Physics2D.OverlapPoint().
Thanks @robertbu - Raycast does not detect 2D colliders - On$$anonymous$$ouseDown() Does detect 2D colliders Does this mean that I cannot achieve my target(to move 2D object using finger touch), Is there any way other than ray that I can move 2D object using finger swipe.