Hi guys! I have a problem with the detecting of touch on the screen of mobile devices. I have a script for my videogame, but it doesn’t work perfectly because when I touch the screen of my smartphone it detect more than one touch. I would that the game detect only one touch when I do it only one. I’m using (Input.touchcount > 0)
How can I do?
Thanks to all!
Your condition in your touch script works every frame. Of course, you detect more touch. Use correct the event touch. I change little your script:
#pragma strict
var sphere : GameObject;
var colors = [Color.red, Color.green, Color.blue, Color.yellow, Color.white];
function Update() {
var hit : RaycastHit;
if (Input.touchCount > 0) {
//Check only for began event only first finger
if (Input.GetTouch(0).phase == TouchPhase.Beganj {
if (Physics.Raycast(transform.position, Vector3.down, hit)) {
Debug.Log(hit.collider.name +", " + hit.collider.tag);
var rend = hit.collider.renderer;
if (rend != null) {
if (rend.material.color == renderer.material.color) {
Debug.Log("Found a match");
Scorecounter.Counter ++;
sphere.renderer.material.color = colors[Random.Range(0, colors.Length)];
}
}
}
}
}
}
I hope that it will help you.