- Home /
How to get 'object touched' (Non-GUI) on iPhone?
Please forgive if asked/answered, so many posts!
I have some in-scene (NOT GUI!) objects I need to know if the user touches (clicks).
In my Win/Mac version I have a collider on my object which repsonds to OnMouseDown
What's the equivalent for iPhone, if any? I've seen the GUI solutions, not what I need.
Is this question different to http://answers.unity3d.com/questions/5941/is-it-possible-to-touch-3d-object-for-using-unity-in-iphone ? It might well be - I'm unfamiliar with Unity iPhone - but it shows up as the top of the related question list, and sounds similar!
Answer by DaveA · Sep 02, 2010 at 03:54 AM
I wanted this to be a 'drop in' replacement for my mousehandler, so here's the script I put on my prefab:
function Update() {
if (Input.GetMouseButtonDown(0)) // check for left-mouse
{
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (collider && collider.Raycast (ray, hit, 100.0))
{
OnMouseDown();
}
}
}
function OnMouseDown() { blah blah }
Answer by jtbentley · Sep 01, 2010 at 11:17 PM
var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 200)) { var objecthit:Transform = hit.transform as Transform;
// What are you hitting?
if (hit.collider.tag == "3DButton") doStuff()
}
Answer by qJake · Sep 02, 2010 at 01:36 AM
You can also use OnMouseDown(), which is easier, but the object has to have a collider, I believe.
Are you sure? I've used On$$anonymous$$ouseDown() on Android... perhaps it's a 3.0-only feature.
That may be. I'm not using 3.0 'seriously' yet as so many things are broken for me.
Your answer
![](https://koobas.hobune.stream/wayback/20220613072519im_/https://answers.unity.com/themes/thub/images/avi.jpg)