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);
}
}
}