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!
1 Answer
1Your 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.
Beautiful! It's perfect for me! Thank you so much! @zharik86 But, I also would to detect when the player doesn't touch the screen in the correct moment. For example when the ball is rolling on the color of its same color..How can I do?
– GabboUnity@GabboUnity I didn't understand about a correct time moment a little. But in your function Update() you already check coincidence of materials of a ball and surface under it, as I see from script.
– zharik86Yes, because the player must touch the screen while the ball is rolling on the plane that is colored like the sphere. I want that, if the player forget to touch the screen, the game stop. I hope that you can understand now.
– GabboUnity@zharik86 it's exactly what I would, but the only problem is that it stops the game at the second time that the ball rolls on the plane that has the same color and it doesn't stop it after the first plane (clearly if the player doesn't touch the screen)..
– GabboUnity@GabboUnity As I understand (in colors). For example, boll is green. It's roll for plane as green. User should be touch, because colors match. If user don't touch, that game over. I modification my preview comment.
– zharik86
Please forrmat you code. Edit your question, select your code, and sue the 101/010 button
– robertbuNow? Is it correct?
– GabboUnityYour code is now readable, but there is nothing here concerning your touch input. We need to see the code that processes your touch input in order to see if we can help with the probelm.
– robertbuNow I wrote the type of touch that I have in my script..
– GabboUnityAs mentioned, we need to see the code that is processing the touch input. Based on what you've described, you could do: if (Input.touchcount == 1) and then if this is true, you can use Input.GetTouch(0) But the solution might be more complicated.
– robertbu