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.
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 instead of Ray, Kindly help me on that?
– aleem.razzaq09Input.mousePosition is Vector3 type, what is alternate function for finger single touch which has Vector3 type because ScreenPointToRay takes Vector2 type. Thanks again.
– aleem.razzaq09@robertbu, Did u get my problem for 2D in Unity4.3. I am still stuck over there.
– aleem.razzaq09Thanks @robertbu - Raycast does not detect 2D colliders - OnMouseDown() 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.
– aleem.razzaq09It means you have to work around it. For example, you could have a a child object that has a box or mesh collider. The child object can detect the touch and move the parent.
– robertbu