Multi-touch game not working
I am creating a 4 player(same screen) basketball game where you shoot balls at a net in the middle. For the life of me I cannot figure out why this is not working, it works if only one finger is used but if another finger presses down the first touch appears to be overridden. I tried making all the variables local in my for loop but then I have to declare them which doesn't make sense because then on the next frame they are reset back to the declared state. I have tried doing this with a single script and with a script on each one of the balls but the touch still seems to get overridden even when I added fingerID checks. Please help a very confused noob coder out! Unity 2019.1.3f1
public class TouchControlsSingle : MonoBehaviour
{
public Vector3 startRotation;
public float thrustMultiplier = 900.0f;
public float heightMultiplier = 3f;
Vector2 startTouch;
Vector2 direction;
float thrust;
Vector3 startPos;
Vector3 endPos;
float startTime;
float endTime;
float swipeTime;
GameObject touchedObject;
private void Update()
{
for (int i = 0; i < Input.touchCount; i++)
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(i);
int fingerID = touch.fingerId;
switch (touch.phase)
{
case TouchPhase.Began:
Ray ray = Camera.main.ScreenPointToRay(touch.position);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
touchedObject = hit.transform.gameObject;
}
startTouch = touch.position;
startPos = Camera.main.ScreenToWorldPoint(Input.touches[i].position);
startTime = Time.time;
transform.eulerAngles = startRotation; //reset x, z rotation, only use y rotation
break;
case TouchPhase.Moved:
direction = touch.position - startTouch;
break;
case TouchPhase.Ended:
int fingerTouch = Input.GetTouch(i).fingerId;
if (fingerTouch == fingerID)
{
endPos = Camera.main.ScreenToWorldPoint(Input.touches[i].position);
endTime = Time.time;
swipeTime = endTime - startTime;
thrust = direction.magnitude / thrustMultiplier;
BallsControls ballsControls = touchedObject.GetComponent<BallsControls>();
ballsControls.Shoot(direction, thrust, swipeTime, heightMultiplier);
}
break;
}
}
}
}
}
Your answer
Follow this Question
Related Questions
Unity 3D UI Button Not Working When Touched (Mobile)? 1 Answer
Touch screen not working on Android build? 0 Answers
Multi-touch game can only shoot 1 ball at a time 0 Answers
How can I block Physical Raycasts from going through UI Elements? 0 Answers
How to disable native input field from Android TouchScreenKeyboard. 0 Answers