- Home /
Unwanted multiple Taps
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.
Is this maybe in FixedUpdate (ins$$anonymous$$d of Update?)
It wouldn't have anything to do with Instantiate, since the IF isn't checking it. I'd feel better with #if UNITY_IOS around parts, but that probably won't matter.
Get$$anonymous$$ouseDown should be working. I'd set up a quick test scene with that to try to find a structural problem (script on two things? ... )
Changing from FixedUpdate() to Update() did reduce the number of unwanted instances, but did not remove them entirely
Answer by I Am The Minion · Apr 05, 2013 at 04:48 PM
Durrr It just hit me. Unity translates touch to click. Everytime I touch the game, it does what it says under both the click AND touch conditions. Thats why I'm getting 2 instances. Excluding the click query should fix this problem.