Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Issue with multi-touch on mobile, please send help<3 [beginner]

Discussion in 'Input System' started by Aldoistaken, Jan 3, 2023.

  1. Aldoistaken

    Aldoistaken

    Joined:
    Dec 10, 2022
    Posts:
    1
    Hey everyone, it's my first time posting on here but the forum has already been a huge help in my short Unity journey. ✨ I'm currently working on a mini-game, it's 90% done but I'm still trying to implement touch controls. On paper it should be super simple, the camera and background are fixed, the character can only move on the X axis, like so :



    It works alright with the keyboard but when I upload my WebGL build on itch.io and give it a try on my iPhone 8, I get a pretty annoying bug. The touch controls work fine for the most part, when I touch the left half of the screen, the character goes to the left, when I touch the right half of the screen, the character goes to the right, which is what I want. The bug arises in that specific scenario :

    - let's say I touch the left side of the screen with my left thumb (finger 1), the character goes left (expected behaviour)
    - then I touch the right of the side of the screen with my right thumb (finger 2), the character goes right (expected behaviour once again)
    - now if I remove my original finger 1 and keep finger 2 on the screen, the character goes back to the left where there are no fingers anymore! That's my issue.

    I guess this has to do with the finger index changing when the overall numbers of touches changes but I can't find a workaround. Here's my current code :

    Code (CSharp):
    1.  
    2. if (Input.touchCount > 0)
    3.  
    4. {
    5.       Touch touch = Input.GetTouch(Input.touchCount - 1);
    6.       Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
    7.  
    8.       if (touchPosition.x < 0)
    9.       {
    10.       playerRigidbody.velocity = Vector2.left * playerSpeed * Time.fixedDeltaTime; //player goes left
    11.       }
    12.  
    13.       if (touchPosition.x > 0)
    14.       {
    15.       playerRigidbody.velocity = Vector2.right * playerSpeed * Time.fixedDeltaTime; //player goes right
    16.       }
    17.  
    18. }
    I feel like this should work in theory, when I remove finger1, finger2 gets reassigned with the index 0 and therefore touch.position.x should be greater than 0, but I guess I'm missing something? I tried to look into fingerID but I don't know how to use it. I also tried to use the Input Manager and got it working but the result was the same and it felt overkill for this project.

    I know that WebGL builds are not actually supported on mobile so could that be the issue here?

    Anyway, sorry for the long explanation! You can play the current version of the game here, the password is "cornflexx". Would appreciate any kind of help, I've been stuck on this for 3 days.