Im trying to code a start button for a 2d game for android but I cannot figure it out. My code currently is: if (Input.touchCount == 1) { Vector3 wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position); Vector2 touchPos = new Vector2(wp.x, wp.y); if (collider2D.OverlapPoint(touchPos)) { //your code //Application.LoadLevel("RunLevel"); msg = Physics2D.OverlapPoint(touchPos).gameObject.transform.tag; } } This should be checking going to the next scene if i press the play button, but if i press anywhere on the screen its returning the collider its attached to.
I'd recommend using raycasts instead, and some better touch-handling, to prevent multitouches (taken from the Unity script reference). Code for touches (cannot test it myself, though): Code (csharp): for (var i = 0; i < Input.touchCount; ++i) { if (Input.GetTouch(i).phase == TouchPhase.Began) { RaycastHit2D hitInfo = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position), Vector2.zero); // RaycastHit2D can be either true or null, but has an implicit conversion to bool, so we can use it like this if(hitInfo) { Debug.Log( hitInfo.transform.gameObject.name ); // Here you can check hitInfo to see which collider has been hit, and act appropriately. } } } ...but I tested this for use with mouse-clicks, and it works perfectly. Code (csharp): if (Input.GetMouseButtonDown (0)) { Debug.Log ("Clicked"); Vector2 pos = new Vector2(Input.mousePosition.x, Input.mousePosition.y); RaycastHit2D hitInfo = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(pos), Vector2.zero); // RaycastHit2D can be either true or null, but has an implicit conversion to bool, so we can use it like this if(hitInfo) { Debug.Log( hitInfo.transform.gameObject.name ); // Here you can check hitInfo to see which collider has been hit, and act appropriately. } } Just remember that your sprite must have some kind of 2D collider on it to be registered.
It's still not working, no matter where I touch on the screen it always returns with the play button? Its weird, the play button is the only box collider on the screen and if I click say the background it returns "playbutton" instead of null.... so weird! If the sprite box collider's size is less then 1 it will only return the backgrounds collider, if its over size of 1 and it only returns the box collider on the play button.
It's like the ray is off or something, if the boundary for the playbutton is over 1 then even if I click outside the collider it returns play button!? If my x scale on box collider is over 1.5, no matter where i press on the screen, it always returns the playbutton, and if I do under 1.3 it doesnt detect anything, even if im right ontop of it.
Yup, I'm officially dumb... haha. I forgot to turn my camera to orthographic for 2d games! DOH! Thanks a ton ultro the code works perfectly.
Heheh, happens to the best And you're very welcome. It's a 3-minute mix-match of 2 different pages in the Unity Script Reference, so it darn well should work Remember that local variables, like that Vector2 pos and the hitInfo, would probably be better off as a global temp-variable, if you're running this script in an Update() method. If the code is very situational, it should be fine the way it is.