- Home /
GUI Textures problem
Hello guys , i,m a begginer in games development , please this trouble killed me , and if i solve it i,ll solve a lot of things and troubles i face it and i,ll achieve my wishes :(
i have many GUITexture , (4) to move my tank forward,back,left and right and (1) GUITexture to shoot fire .
how i can click more than one GUITexture and execute them together , the troublie is : when i click on the first GUITexture & second GUITexture together , no one of them will run .
i,ll make this GUITexture As toutch buttons for my game On Android .
My Code :
if(Input.GetMouseButton(0) && forwardTexture.HitTest(Input.mousePosition)){
transform.Translate(Vector3.forward * Time.deltaTime*speed);
}
if(Input.GetMouseButton(0) && BackTexture.HitTest(Input.mousePosition)){
transform.Translate(Vector3.back * Time.deltaTime*speed);
}
if(Input.GetMouseButton(0) && leftTexture.HitTest(Input.mousePosition)){
transform.Rotate(0,-Time.deltaTime*RotateL,0);
}
if(Input.GetMouseButton(0) && rightTexture.HitTest(Input.mousePosition)){
transform.Rotate(0,Time.deltaTime*RotateR,0);
}
(shoot ) < in another script
if(Input.GetMouseButtonDown(0) && ShootTexture.HitTest(Input.mousePosition))
{
SHooT();
}
Answer by Tekksin · Jun 05, 2014 at 10:55 PM
You have to set up a for loop to recognize all the buttons being touched, as well as the phases of touch that they are in.
So what you would do is apply a script called "TouchButtonLogic" (or whatever) to all of your gui buttons, and in it, just do something like:
function Update(){
if(Input.touches.Length <= 0){ //this tells the device there are no touches
//the controls do nothing.
//you may want to reference this script in your other scripts, and control everything through booleans.
}
if(Input.touches.Length >= 1){ //this tells the device that a touch is happening.
for(var i = 0; i < Input.touchCount; i++){ //this goes through a loop and assigns a number to each subsequent touch.
var touch : Touch = Input.GetTouch(i);
if(touch.phase == TouchPhase.Began && guiTexture.HitTest(Input.GetTouch(i).position)){ //this is for when the touch begins
//set the appropriate information here.
//again, choose to set booleans in this script and then reference to the already documented info you have in other scripts. It's your choice.
} else if(touch.phase == TouchPhase.Stationary && guiTexture.HitTest(Input.GetTouch(i).position)){
//This is what the device will do if the finger is held down, which if you're moving something by holding down a key, it must be activated here.
}else if(touch.phase == TouchPhase.Ended && guiTexture.HitTest(Input.GetTouch(i).position)){
//this is where you tell the device to stop doign whatever you've told it to do in Begin and especiall stationary.
}
}
}
}
where I added the /commented out information/ be sure to address the object the script is on, but doing something like:
if(name == "guiTexture Name"){
//do this code for that button.
}
if(name == "Another guiTexture Name"){
//do this for the other button
}
//and so on.
The unity docs is an excellent resource to do what you're trying here: http://docs.unity3d.com/Manual/MobileInput.html
read it over if my comment confused you in some way. It is essentially the same info. Good luck!
thanks a lot dear , but can you make your script compatable with my script and my GUITextures name , because i,m not good of english and program$$anonymous$$g and i cant understand this good :(
function Update(){
if(Input.touches.Length <= 0){
//tell it to do nothing
}
if(Input.touches.Length >= 1){
for(var i = 0; i < Input.touchCount; i++){
var touch : Touch = Input.GetTouch(i);
if(touch.phase == TouchPhase.Began && guiTexture.HitTest(Input.GetTouch(i).position)){
if(name == leftTexture){
//put your code here for the left button.
}
if(name == rightTexture){
//put your code here for the right button. "rightTexture" is what you named the guiTexture GameObject.
}
} else if(touch.phase == TouchPhase.Stationary && guiTexture.HitTest(Input.GetTouch(i).position)){
}else if(touch.phase == TouchPhase.Ended && guiTexture.HitTest(Input.GetTouch(i).position)){
}
}
}
}
you cannot use "getmousebutton(0)" for multiple touches.