- Home /
Problem with touch
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!
Now I wrote the type of touch that I have in my 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) {
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)];
}
} }
}
This is the script in which I use the checking of the touch..
Um, you're not using the touch position at all in your code.
Answer by zharik86 · Oct 22, 2014 at 06:28 AM
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.
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 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.
Yes, 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 As I understood, check if colors of a ball and the plane match is necessary for you. And if the user managed to touch the screen. Then we will make this check. I hope that the script is attached to a ball:
#pragma strict
var sphere : GameObject; //What do variable?
var colors = [Color.red, Color.green, Color.blue, Color.yellow, Color.white];
var my$$anonymous$$atch: boolean = false; //variable for match colors, our plane
var myPlane: Transform = null; //variable for plane
//Function for check match colors, return value true/false
function CheckPlane(): boolean {
var hit : RaycastHit;
var tpCheck: boolean = false;
if (Physics.Raycast(transform.position, Vector3.down, hit)) {
//UPDATE2: use better hit.transform
var rend = hit.transform.renderer;
if (rend != null) {
//UPDATE: add check variable match
if ((rend.material.color == renderer.material.color || my$$anonymous$$atch) && myPlane != hit.transform) {
Debug.Log("Found a match in check");
myPlane = hit.transform;
tpCheck = true;
}
}
}
return tpCheck;
}
function Update() {
//Find change for plane every frame and check match/no match
var tpCheck = this.CheckPlane();
if (my$$anonymous$$atch) {
if(tpChech) {
//If you have match and function return true, thus you change plane: game over
//GA$$anonymous$$E OVER - your function and etc
//UPDATE2: for game over stop time
Time.timeScale = 0;
}
} else {
my$$anonymous$$atch = tpCheck;
}
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);
//UPDATE2: use hit.transform
var rend = hit.transform.renderer;
if (rend != null) {
if (rend.material.color == this.renderer.material.color) {
Debug.Log("Found a match");
Scorecounter.Counter ++; //your numerator?
//UPDATE2: replace sphere.renderer on this.renderer and attach script at your ball
this.renderer.material.color = colors[Random.Range(0, colors.Length)];
my$$anonymous$$atch = false; //Touch is began and all check, than value is false
}
}
}
}
}
}
I hope works.
@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)..