My script instantiates a prefab at point of click or touch, every time it is clicked or touched. The problem I am having, is that more than one instance of the prefab is created with every single touch. I managed to solve it for the click version.
Initially, i did this:
if(Input.GetMouseButtonDown (0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay (ray.origin, ray.direction * 1000, Color.yellow);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 1000))
{
GameObject newBomb = (GameObject)Instantiate (bomb, hit.point, Quaternion.identity);
bombOrder.Add (newBomb);
}
}
if(Input.touchCount == 1 && Input.GetTouch (0).phase == TouchPhase.Began)
{
//place bomb at touch location
Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
RaycastHit hit;
if(Physics.Raycast (ray, out hit, 1000))
{
GameObject newBomb = (GameObject)Instantiate (bomb, hit.point, Quaternion.identity);
//add bomb to list
bombOrder.Add (newBomb);
}
}
When I changed GetMouseButtonDown
to GetMouseButtonUp
, that solved the problem. I tried the same approach on the touch-version by changing TouchPhase.Began
to TouchPhase.Ended
. No change, unfortunately. I’m still getting at least 2 instances per touch. Could it be a performance issue? I’m thinking if there is a slight lag-spike on instantiation, it might read multiple touches.