Need help with touch controls for an android game.
Hello guys I am new to unity and I am making a game where there will be two bouncy sprites spawning from either side of the screen according to the touch on both sides. I was able to make the bouncy sprite spawn on tap at both sides of the screen ( I have divided the screen into two touch areas , left sprite spawn when left side is touched and right is spawned when the right is touched). Below is the code for left paddle :
var Leftpaddle : GameObject;
function Update ()
{
if (Input.touchCount>0)
{
var touch : Touch = Input.GetTouch(0);
if(touch.position.x < Screen.width/2)
{
this.GetComponent.<Renderer>().enabled = true;
this.GetComponent.<EdgeCollider2D>().enabled = true;
}
}
else
{
this.GetComponent.<Renderer>().enabled = false;
this.GetComponent.<EdgeCollider2D>().enabled = false;
}
if (Input.touchCount>0)
{
var touch2 : Touch = Input.GetTouch(0);
// var touch3: Touch = Input.GetTouch(0);
if( touch2.position.x > Screen.width/2 && touch2.position.x < Screen.width/2 )
{
this.GetComponent.<Renderer>().enabled = false;
this.GetComponent.<EdgeCollider2D>().enabled = false;
}
}
}
and this is the code for right paddle var paddle : GameObject;
function Update ()
{
if (Input.touchCount>0)
{
var touch : Touch = Input.GetTouch(0);
if(touch.position.x > Screen.width/2)
{
this.GetComponent.<Renderer>().enabled = true;
this.GetComponent.<EdgeCollider2D>().enabled = true;
}
}
else
{
this.GetComponent.<Renderer>().enabled = false;
this.GetComponent.<EdgeCollider2D>().enabled = false;
}
if (Input.touchCount>0)
{
var touch2 : Touch = Input.GetTouch(0);
// var touch3: Touch = Input.GetTouch(0);
if( touch2.position.x > Screen.width/2 && touch2.position.x < Screen.width/2 )
{
this.GetComponent.<Renderer>().enabled = false;
this.GetComponent.<EdgeCollider2D>().enabled = false;
}
}
}
Now the problem is , when I touch both sides of the screen the sprites(paddles) are not getting disabled. Any one of them still spawns and when I press and hold on left / right side spawning a side and when i tap on the other side the other side spawns too making both paddles spawn on their places.
Couldn't figure out what is the problem. Any help is appreciated. Thanks
Answer by g__l · Dec 30, 2015 at 11:09 AM
First of all you could create two buttons for left and right and make their colour 0 in all colour selectors in the inspector, so it will be invisible. Alternativly you could use the Array Input.touches and create code which determins if a touch is left so -x or right so +x coordinate. Either should work.
Note - In the inspector only one button can be pressed at a time but when you build the game for android it will work fine.
Your method worked. thank you for that. Can you tell me how can I make only one button work at a time when deployed to android because I don't want both buttons to work when I press both of them at the same time.
$$anonymous$$aybe in your functions for each buttons you could put an if statement around the code. Then make a single variable (bool) that goes true when a button is pushed. Then in your if statement make it so that the code is only completed if the variable is false (one button is not pushed already, so you get the desired result of one button being pushed at a time. Hope it helps.