- Home /
How to select gameobjects on android by touching on them?
I am trying to create match 3 game like candy crush while learning unity. How can i store references of two gameobjects(object) which are to be swapped on android device? On computer it is working fine i did it using onMouseDown() on gameobject and having two gameobject references to store selected objects in gamecontroller script. Thanks for any help and suggestion and sorry for bad English.
Thanks Guppie1337 I will check this out. Does this works with 3D objects? Can you explain ScreenToWorldPoint part please i am not getting it.
No worries! Basically what ScreenToWorldPoint does is it takes the position of your screen and converts it into a world point. World points (The scene) work off of coordinates like (1,5). Screen points I believe either work off of pixel points (essentially the same thing but for the screen) or viewport points (which are any value for x and y from 0 to 1). Here it is in the Scripting API - ScreenToWorldPoint.
To answer your question about if this works in 3D. From what I know, yes. The difference is that you wouldn't convert it to a vector 2 but ins$$anonymous$$d use all 3 original coordinates. The whole depth concept tends to throw me off though.
If this answers your question, please tick it :)
Answer by Guppie1337 · Jun 14, 2015 at 12:40 PM
If you are asking just how to touch these objects:
if (Input.touchCount == 1)
{
Vector3 worldPos = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
Vector2 touchPos = new Vector2(worldPos.x, worldPos.y);
if (collider2D == Physics2D.OverlapPoint(touchPos))
{
//Do stuff with it here like check gameObject tags and such.
}
}
Your answer
Follow this Question
Related Questions
Game Object On Touch Event on Android-iPhone 2D 4 Answers
Select 2 out of 3 gameobject before doing something. 0 Answers
Touch.deltaPosition giving jerky movement on Android in Unity 5 2 Answers
How to destroy a particular clone on touch/mouse? 1 Answer
Input.GetTouch(0).deltaPosition.x returns 0 sometimes 0 Answers