0
\$\begingroup\$

We are trying to make 2 virtual joysticks for both sides of the screen. Movement joystick can be enabled at any pressed position of the left side of the screen and shooting joystick can be enabled on right side.

So far we are trying to make second input to work when movement joystick is our first input. But it feels like second input is locked on the left side of the screen.

What happens when we try to move left side joystick as a first input and then press on the right side of the sceen with other finger (Second input) is that movement joystick locks and player just move to the last position first finger was pointing. Shooting is not happening.

Gameplay, that maybe will help to understand the issue

Code we are working with:

public bool LeftSideActive(int input)
{
    if ((Input.GetTouch(input).position.x >0) && (Input.GetTouch(input).position.x < halfScreenWidth))
    {
        if(Input.GetMouseButtonDown(0)) {
            LeftSideTouched = true;
            Debug.Log("Left side enabled: " + LeftSideTouched);
        }
        if(Input.GetMouseButtonUp(0)) {
            LeftSideTouched = false;
            Debug.Log("Left side enabled: " + LeftSideTouched);   
        }
    }
    else if ((Input.GetTouch(input).position.x >halfScreenWidth && LeftSideTouched)){
        if(Input.GetMouseButtonUp(0)) {
            LeftSideTouched = false;
        }                   
    }
    return LeftSideTouched;
}

public bool RightSideActive (int input)
{
    if ((Input.GetTouch(input).position.x > halfScreenWidth) && (Input.GetTouch(input).position.x < Screen.width)) 
    {
        if(Input.GetMouseButtonDown(0)){
            RightSideTouched = true;    
            Debug.Log("Right side enabled: " + RightSideTouched);    
        }
        if(Input.GetMouseButtonUp(0)) {
                RightSideTouched = false;
                Debug.Log("Right side enabled: " + RightSideTouched);  
            }    
    }
    else if ((Input.GetTouch(input).position.x < halfScreenWidth && RightSideTouched)){
            if(Input.GetMouseButtonUp(0)) {
                RightSideTouched = false;
            }                 
    }
    return RightSideTouched;

}

public void LeftSideControls(int input){
    if ((Input.GetTouch(input).position.x >0) && (Input.GetTouch(input).position.x < halfScreenWidth))
    {
        if(Input.GetMouseButtonDown(0))
        {
            LeftSideTouched = true;
            movementA = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
            this.transform.position = movementA; //Updates position where joystick will appear

        }
        if(Input.GetMouseButtonUp(0))
        {
            LeftSideTouched = false;
        }

        // Sets new finger position on drag
        movementB = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
    }
    else if ((Input.GetTouch(input).position.x > halfScreenWidth && LeftSideTouched)){
            if(Input.GetMouseButtonUp(0)){                
            }
            //Sets new finger position on drag; When finger is dragged from left to right side of the screen controls are kept.
            movementB = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z)); 
    }
}

public void RightSideControls(int input){
    if ((Input.GetTouch(input).position.x > halfScreenWidth) && (Input.GetTouch(input).position.x < Screen.width)) 
    {
        if(Input.GetMouseButtonDown(0)){
            RightSideTouched = true;
            shootingA = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
            this.transform.position = shootingA; //Updates position where joystick will appear
        }

        if(Input.GetMouseButtonUp(0))
        {
            RightSideTouched = false;
        }
        shootingB = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z)) - transform.position;
        shootingAngle =  Mathf.Atan2(shootingB.y,shootingB.x)*Mathf.Rad2Deg;
        playerGun.transform.rotation = Quaternion.Euler(0,0,shootingAngle);
    }
    else if ((Input.GetTouch(input).position.x < halfScreenWidth && RightSideTouched)){
        if(Input.GetMouseButtonUp(0)){
            RightSideTouched = false;                 
        }
        //Sets new finger position on drag; When finger is dragged from right to left side of the screen controls are kept.
        shootingB = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z)) - transform.position; 
        shootingAngle =  Mathf.Atan2(shootingB.y,shootingB.x)*Mathf.Rad2Deg;
        playerGun.transform.rotation = Quaternion.Euler(0,0,shootingAngle);
    }
}
public void  JoystickEnabling()
{


    while( Input.touchCount > 0)    
    {
        if(Input.touchCount>=3)
        {
            Debug.Log("More than 2 touches!!!!");
            return;
        }

        if(Input.touchCount == 1)
        {
            Debug.Log("Touch-count 1");
            FirstFinger = 0;
            SecondFinger = FirstFinger;            

            if(LeftSideActive(FirstFinger)&&!RightSideActive(FirstFinger))
            {
                JoystickSpriteRenderer(moveJoyRenderer, true);
                JoystickSpriteRenderer(shootJoyRenderer, false);
                LeftSideControls(FirstFinger);
                //Debug.Log("LeftSideTouchy");
            }
            else if(!LeftSideActive(FirstFinger)&&RightSideActive(FirstFinger))
            {
                JoystickSpriteRenderer(moveJoyRenderer, false);
                JoystickSpriteRenderer(shootJoyRenderer, true);
                RightSideControls(FirstFinger);
                //Debug.Log("RightSideTouchy");
            }
            else{
                JoystickSpriteRenderer(moveJoyRenderer, false);
                JoystickSpriteRenderer(shootJoyRenderer, false);
            }
        }





        else if (Input.touchCount == 2)
        {
            Debug.Log("Touch-count 2");
            FirstFinger = 0;
            SecondFinger = 1;


            if (LeftSideActive(FirstFinger)){
                LeftSideControls(FirstFinger);
                if(LeftSideActive(SecondFinger)){
                    Debug.Log("Second left");
                    return;
                }
                else if(RightSideActive(SecondFinger)){
                    Debug.Log("LEft and Right");
                    RightSideControls(SecondFinger);
                }
            }
        }

        break;
    }
}
    void FixedUpdate(){
        if(LeftSideTouched){
            //Calculates offset between first and last finger position
            Vector2 offset = movementA - movementB;
            Vector2 direction = Vector2.ClampMagnitude(offset, 1.0f);
            //Moves character
            MoveCharacter(direction * -1f);
        }
    }

      void MoveCharacter(Vector2 direction) // Function for character movement
    {
        player.Translate(direction * speed * Time.deltaTime);
    }

    //Enable either joystick sprite
    void JoystickSpriteRenderer(SpriteRenderer joystick, bool enable){
        joystick.GetComponent<SpriteRenderer>().enabled = enable;
    }

}
\$\endgroup\$
0
\$\begingroup\$

I have something to share about touch controls.

Perhaps this is a similar confusion of Unreal Engine having touch1, touch2 as first finger touch and second finger touch inputs. Unless you add and map touch2, only first touch/swipe input is taken as input.

After a quick visit to Unity docs, a similar system is applied as Input.GetTouch(1) and Input.GetTouch(2) as 2 finger gestures.

I have never used a joystick but perhaps you are facing similar troubles. Hope this solves your problem.

Good Luck.

\$\endgroup\$
  • \$\begingroup\$ So as I understood, you have to make 3 touch inputs to make multi touch possible? \$\endgroup\$ – Tadas Jan 8 at 18:40
  • \$\begingroup\$ If you need 3 finger input(like screen zooming + shooting at same time) then go for 3 inputs. For a racing game with left side for boosts right for steering, its 2 inputs. For something like ninja slice or candy crush 1 is enough. \$\endgroup\$ – suvam0451 Jan 10 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.