Search Unity

Resolved Multi-touch in Input Actions

Discussion in 'Input System' started by WeanWind, May 21, 2023.

  1. WeanWind

    WeanWind

    Joined:
    Feb 14, 2023
    Posts:
    5
    Hello. I'm new to Input System and programming in general. I'm trying to make multi-touch to work with my input system setup.

    I have 2 on-screen sticks. One on the left side and one on the right side of the touchscreen. When player touches two or one of the sides on the screen those on-screen sticks appear, and player will be able to control the camera and/or the player.

    The problem is with current setup is that I'm able to control only one of the sticks. I'm guessing it has something to do with my Input Actions. But I'm not sure. How do I determine one touch and the other without inflicting another in my InputActions?

    upload_2023-5-21_22-43-35.png

    My code:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3. using UnityEngine.UI;
    4.  
    5. public class JoystickController : MonoBehaviour
    6. {
    7.     // Movement joystick
    8.     public GameObject LeftJoystickObject;
    9.     public Image LeftJoystickOutline;
    10.     public Image LeftJoystick;
    11.     private bool Pressed;
    12.     private bool LeftSide;
    13.     //----------------------
    14.  
    15.     // Camera joystick
    16.     public GameObject RightJoystickObject;
    17.     public Image RightJoystickOutline;
    18.     public Image RightJoystick;
    19.     private bool Touched;
    20.     private bool RightSide;
    21.     //----------------------
    22.  
    23.     private void Start()
    24.     {
    25.         // Movement joystick
    26.         LeftJoystickObject.SetActive(true);
    27.         LeftJoystickOutline.enabled = false;
    28.         LeftJoystick.enabled = false;
    29.         Pressed = false;
    30.         LeftSide = false;
    31.         //----------------------
    32.  
    33.         // Camera joystick
    34.         RightJoystickObject.SetActive(true);
    35.         RightJoystickOutline.enabled = false;
    36.         RightJoystick.enabled = false;
    37.         Touched = false;
    38.         RightSide = false;
    39.         //----------------------
    40.     }
    41.  
    42.     //-----------------------------------------------------------------------------------------------
    43.  
    44.     void OnLeftTouchPosition(InputValue Value)
    45.     {
    46.         Vector2 Touch1Pos = Value.Get<Vector2>();
    47.  
    48.         if (Touch1Pos.x < (Screen.width / 3) && Touch1Pos.x > 150 && Pressed == false)
    49.         {
    50.             LeftSide = true;
    51.             LeftJoystickObject.transform.position = Touch1Pos;
    52.         }
    53.     }
    54.  
    55.     void OnLeftTouchPress(InputValue Value)
    56.     {
    57.         if (LeftSide == true)
    58.         {
    59.             Pressed = true;
    60.             LeftJoystickOutline.enabled = true;
    61.             LeftJoystick.enabled = true;
    62.         }
    63.     }
    64.  
    65.     void OnLeftTouchRelease(InputValue Value)
    66.     {
    67.         Pressed = false;
    68.         LeftJoystickOutline.enabled = false;
    69.         LeftJoystick.enabled = false;
    70.         LeftSide = false;
    71.     }
    72.  
    73.     //-----------------------------------------------------------------------------------------------
    74.  
    75.     void OnRightTouchPosition(InputValue Value)
    76.     {
    77.         Vector2 Touch2Pos = Value.Get<Vector2>();
    78.  
    79.         if (Touch2Pos.x > (Screen.width / 2) && Touched == false)
    80.         {
    81.             RightSide = true;
    82.             RightJoystickObject.transform.position = Touch2Pos;
    83.         }
    84.     }
    85.  
    86.     void OnRightTouchPress(InputValue Value)
    87.     {
    88.         if (RightSide == true)
    89.         {
    90.             Touched = true;
    91.             RightJoystickOutline.enabled = true;
    92.             RightJoystick.enabled = true;
    93.         }
    94.     }
    95.  
    96.     void OnRightTouchRelease(InputValue Value)
    97.     {
    98.         Touched = false;
    99.         RightJoystickOutline.enabled = false;
    100.         RightJoystick.enabled = false;
    101.         RightSide = false;
    102.     }
    103.  
    104.     //-----------------------------------------------------------------------------------------------
    105. }
    106.  
     
  2. WeanWind

    WeanWind

    Joined:
    Feb 14, 2023
    Posts:
    5
    I have decided to go with a different approach by simply making right stick always on and invisible (image alpha 0). But I'm keeping thread active. That's more like avoiding the problem rather than fixing.
     
  3. WeanWind

    WeanWind

    Joined:
    Feb 14, 2023
    Posts:
    5
    Messy fix. But it works.

    upload_2023-5-23_2-11-49.png