Need help with touch implementation in unity

Started by
2 comments, last by rishabh91 2 months, 2 weeks ago

I am implementing multitouch feature for a mobile game where user(s) can move certain game objects by touch and drag. Using the following code, I am able to achieve this behaviour but with an issue i.e If I touch and hold anywhere else on screen, then Im not able to move the intended gameobject using other finger. If I touch and move gameobject1 with one finger then at same I can successfully touch and move gameobject2 with other finger. The problem only comes if I touch and hold anywhere else on screen, then I can't touch/move intended gameobjects using other finger. Please guide me how to approach this problem.

using UnityEngine;
using UnityEngine.InputSystem.EnhancedTouch;
using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;
using TouchPhase = UnityEngine.InputSystem.TouchPhase;

    void Start()
    {
        EnhancedTouchSupport.Enable();
        SetBoundaries();
    }

    void Update()
    {
       TouchInput();
    }

    void TouchInput()
    {
        if(!isDragging)
        return;
        
        foreach (Touch touch in Touch.activeTouches)
        {
    
            Vector3 touchPos = touch.screenPosition;
            touchPos = Camera.main.ScreenToWorldPoint(touchPos);

            RaycastHit2D hit = Physics2D.Raycast(touchPos, Vector2.zero);

            if (hit.collider!= null && hit.collider.gameObject == gameObject && touch.phase == TouchPhase.Moved)
            {
                transform.position = new Vector3(touchPos.x , touchPos.y , 0);
                transform.position = new Vector3(Mathf.Clamp(transform.position.x, minBound.x, maxBound.x),
                                            Mathf.Clamp(transform.position.y, minBound.y, maxBound.y), 0);
            }
        }
    }
Advertisement

Never programmed touch, but basically, if code doesn't do what you expect, there are (in this case) 3 different spots where things can go wrong.

  1. Input into the code can be different from what you expect.
  2. Code computes wrong values or takes incorrect “if” branches, ie behaves different than you think it does or should.
  3. Your idea of how the touch system works is not what it really does.

To find out what causes it, you have to debug the code, and find the point where its behavior deviates from your expectations. Once you have the precise point, the decision of what causes it and how to correct it, is much easier than the current “it fails somehow but no clue”.

One way to debug would be to log the details of what happens while trying toe use touch, and analyze the log afterwards on whether what happened matches with your expectations.

@Alberth Ok I analysed a lot and concluded maybe this is how touch system works. I checked another published game which had similar touch interactions and there also similar problem was there but since that game was meant for a single touch so user would not do multitouch because how the game is meant to be played. In my game however, multitouch is essential so I had to rectify the issue.

So, I just created a logic to sense the touch only when the user touches the gameobject. I did this by creating an event trigger (onPointerDown). So, now if user touches anywhere else on screen, the touch sensing function is not enabled.

This topic is closed to new replies.

Advertisement