- Home /
Find a Touch in the BoxCollider
I need to figure out if the user has touched an object. It has a collider. With a mouse I can use OnMouseUp and OnMouseDown on the object. None of these exist for touch.
The Touch object has a screen position Vector2. The collider has a bounds in a 3D space with a contains function in that space.
How can I determine if the Touch was on the object?
Comment
Answer by skovacs1 · Sep 15, 2010 at 09:38 PM
Raycast.
You have the screen position of the touch. Convert it to a ray in the world and raycast to find out if anything was collided with.
Something like:
var selection : Transform;
var hit : RaycastHit;
var ray : Ray = camera.ScreenPointToRay(Vector3(Touch.x,Touch.y,0));
if(physics.Raycast(ray, hit) && hit.transform.tag == "selectable")
selection = hit.transform;
You could write actual event callbacks, but you would pretty much do this either way.