Multi-touch game can only shoot 1 ball at a time
Fairly new to Unity(still suck at coding!) but loving how easy it is to make games. Unity 2019.1.3f1
I am creating a 4-player (same screen) basketball game for Android where the players swipe to shoot balls at the net in the middle of the screen. My script works for shooting a single ball but as soon as there is 2 fingers on the screen it doesn't work or seemingly the first touch gets overridden.
I have tried doing this many different ways, the most current I am trying to create a for loop copied directly from the documentation. I have tried putting a script on each one of the balls and I have also tried creating a singular script that controls all the balls.
I have added fingerId checks and raycast to get the gameobject to make sure the touch points only shoot the balls they are touching but still only 1 ball can be shot at a time. Not sure what else to do to get this working. Attached my whole script that I was putting on each ball.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TouchControls1 : MonoBehaviour
{
Transform lookatTarget;
Rigidbody rb;
TrailRenderer ballTrail;
AudioSource audioSource;
public Color playerColor = Color.red;
//public AudioClip ballBounce;
//public AudioClip rimHit;
Vector2 startTouch;
Vector3 startPos;
Vector2 direction;
Vector3 endPos;
float startTime;
float endTime;
float swipeTime;
private Camera cam;
Vector3 directionVector;
public Vector3 startVector;
public Vector3 startRotation;
float thrust;
public float thrustMultiplier = 900.0f;
public float waitTrimer = 1f;
bool colliding = false;
bool resetingBall = false;
bool ballShot = false;
bool goal1 = false;
bool goal2 = false;
int score = 0;
public Text scoreText;
int shotsOnGoal = 0;
public Text shotsText;
GameObject touchedObject;
bool touched = false;
public float heightMultiplier;
private void Start()
{
cam = Camera.main;
rb = this.gameObject.GetComponent<Rigidbody>();
lookatTarget = this.gameObject.GetComponent<Transform>();
ballTrail = this.gameObject.GetComponent<TrailRenderer>();
audioSource = this.gameObject.GetComponent<AudioSource>();
ballTrail.material.color = playerColor;
scoreText.text = score.ToString();
shotsText.text = shotsOnGoal.ToString();
scoreText.color = playerColor;
shotsText.color = playerColor;
}
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:
//When a touch has first been detected, record the starting position
Ray ray = Camera.main.ScreenPointToRay(touch.position);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
touchedObject = hit.transform.gameObject;
touched = true;
//print("touch = " + touch.fingerId);
}
// Record initial touch position.
startTouch = touch.position;
startPos = Camera.main.ScreenToWorldPoint(Input.touches[i].position);
startTime = Time.time;
//set start rotation
transform.eulerAngles = startRotation; //reset x, z rotation, only use y rotation
if (ballShot == false)
{
colliding = false;
resetingBall = false;
}
break;
//Determine if the touch is a moving touch
case TouchPhase.Moved:
// Determine direction by comparing the current touch position with the initial one
//direction = touch.position - startPos;
direction = touch.position - startTouch;
break;
case TouchPhase.Ended:
int fingerTouch = Input.GetTouch(i).fingerId;
if (fingerTouch == fingerID)
{
//print("fingerTouch = " + fingerTouch);
if (touchedObject == this.gameObject)
{
// Report that the touch has ended when it ends
endPos = Camera.main.ScreenToWorldPoint(Input.touches[i].position);
endTime = Time.time;
ShootBall();
}
}
break;
}
}
}
}
void ShootBall()
{
swipeTime = endTime - startTime;
ballShot = ballTrail.emitting = true;
thrust = direction.magnitude / thrustMultiplier; //calculate thrust
print("thrust = " + thrust);
touched = rb.isKinematic = false;
//add thrust
rb.velocity = new Vector3(direction.x * thrust, (swipeTime / thrust) * heightMultiplier, direction.y * thrust);
print("rb.velocity = " + rb.velocity);
shotsOnGoal++;
shotsText.text = shotsOnGoal.ToString();
}
void OnCollisionEnter(Collision collision)
{
colliding = true;
if (collision.gameObject.name == "Floor")
{
/*audioSource.clip = ballBounce;
audioSource.Play();*/
StartCoroutine("ResetBall");
}
if (collision.gameObject.name == "Rim")
{
/*audioSource.clip = rimHit;
audioSource.Play();*/
}
}
private void OnTriggerEnter(Collider other)
{
colliding = true;
if (other.gameObject.name == "KillZ")
{
StartCoroutine("ResetBall");
}
if (other.gameObject.name == "Goal (1)")
{
goal1 = true;
}
if (other.gameObject.name == "Goal (2)")
{
goal2 = true;
}
if (goal1 && goal2 == true)
{
score++;
scoreText.text = score.ToString();
}
}
IEnumerator ResetBall()
{
if (colliding == true && resetingBall == false)
{
resetingBall = true;
yield return new WaitForSeconds(waitTrimer);
rb.isKinematic = true;
ballTrail.emitting = false;
transform.position = startVector;
transform.rotation = Quaternion.identity;
ballShot = false;
goal1 = false;
goal2 = false;
}
}
}