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.

  • What does it mean "does not work at all"? Any errors? or silently just does not react to anything? - Alexey Shimansky
  • @ Alexey Shimansky Compiled in APK, launched, the keys do not work. How can something like a joystick to attach to these two characters? Each character has its own script. If through the cycle to do, as now, one press will not interrupt the control of another? - Dmitrii
  • Something you have somewhere cant in the coordinates is possible ... Because my tests of your code showed at least some, but working .... s45.radikal.ru/i107/1605/3b/87ecae2d6f9d.gif .. .. I recommend to put some kind of debugging, visual, to see where some coordinates are ..... it’s trite to display the Text UI ... I also want to say that touch has phases. Begin/Moved/Stationary/Ended .... you should rely on them ...... also instead of HitTest you can try if (Right1.GetScreenRect().Contains(touch.position)) { etc .. .... I also recommend Unity3d to Unity3d 5. - Alexey Shimansky
  • Because version 4 is already in the past. right now everything is done on 5 ........ also download the android emulator on the network for this purpose ... I am using the Nox app player - it allows you to download apk , 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
  • one
    Make control buttons via RepatButtons. - NoEscape0

0