Good day! Made a simple game with control of two players on wasd and arrows. In this implementation on the PC, everything works fine.
void Update () { if (Input.GetKey(KeyCode.RightArrow)) dir = Vector2.right; else if (Input.GetKey(KeyCode.DownArrow)) dir = -Vector2.up; else if (Input.GetKey(KeyCode.LeftArrow)) dir = -Vector2.right; else if (Input.GetKey(KeyCode.UpArrow)) dir = Vector2.up; Then I decided to transfer this case to android. Accordingly, I created sprites of arrows, attached GUITexture to them, and wrote the following code
public GUITexture Up1; public GUITexture Down1; public GUITexture Left1; public GUITexture Right1; void Update() { int count = Input.touchCount; for (int i = 0; i < count; i++) { Touch touch = Input.GetTouch(i); if (Up1.HitTest(touch.position)) dir = Vector2.up; if (Down1.HitTest(touch.position)) dir = - Vector2.up; if (Left1.HitTest(touch.position)) dir = Vector2.left; if (Right1.HitTest(touch.position)) dir = Vector2.right; } In this implementation, nothing works and in general, if I understood correctly, this does not handle multitouch.
I saw ready-made joysticks, but they are mostly on Unity3d and I don’t imagine how to fasten them on 2d and what to write at the same time.
I saw solutions through ray, but I have been studying unity very recently, therefore how ray works at all is not clear yet. As far as I understand, they look at the coordinates of tapas, and I need to poke a drawn up arrow - and it works the same way when I press the up key on the PC.
Text UI... I also want to say thattouchhas phases.Begin/Moved/Stationary/Ended.... you should rely on them ...... also instead ofHitTestyou can tryif (Right1.GetScreenRect().Contains(touch.position)) {etc .. .... I also recommendUnity3dtoUnity3d5. - Alexey ShimanskyNox app player- it allows you to downloadapk, emulate clicking on the screen anywhere (I showed it on gif 'ke). more convenient ... well, either connect the androyd directly to the unity, so as not to pervert ..... but now you cant somewhere else or else in the coordinates - Alexei Shimansky