How is it possible to detect if a Touch is touching an object? I tried to compare the positions without any luck…
I think you can use OnMouseDown (). I’m pretty sure its functionality was changed to support touches on Iphone.
You will need to shoot a ray from screen space to world space. Luckily Unity made this very easy for you:
Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit[] hitInfo;
hitInfo = Physics.RaycastAll(mouseRay, Mathf.Infinity);
hitInfo will contain all the objects you are touching, not just the closest one.
You could rewrite this to use touches for multitouch support.[/code]
Thanks, but I cant get this to work, how would I make the check to see if it is the object which has the script on it, that i’m currently touching?
Well you could go the other way around, by checking from ‘within’ the object if its being touched.
It would be something like:
Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if(collider.Raycast(mouseRay, out hitInfo, Mathf.Infinity))
{
//Mouseover this gameobject/collider!
}
That worked
However is there a way to make it more sensetive? the object has to be hit very precisely to make it respond.
You could make the collider bigger so you can touch it easier ![]()
Yea but that would also make it easier for other objects to hit it. So I tried to make an empty child with a bigger collider and then try to get the childs collider instead.
It work when using the finger, but the other objects react to the child too. Is there a way for them to ignore the childs collider?
You can use Physics.IgnoreCollision(childCollider, everyoneElse) or an easier method would be to make the child collider a trigger, because rays collide with triggers, but normal objects don’t so the rest of your objs would simply pass through the bigger collider.
The trigger solved it
thanks