- Home /
GUI texture touch input problem
Hi all. Ok I've googled and browsed through Unity Answers all day endlessly clicking on related topics till they were just repeating and I just can't wrap my head around making basic GUI texture buttons that allow more than one being pressed at the same time, and basically work like WASD on the keyboard, but ofc I have problems.
The thing is, for example, the script basically controlls the upClicked boolean and returns true if its touched and false if its not, which in turn controls the player, lander style ship in this case. Now a single touch on it works fine, but the problem is when for example i touch up to fly up, and while touching up i also touch left to fly up and left, it does start flying up and left BUT, when i now release up while holding left, it should should make upClicked false and just go left but it doesn't, instead it continues going up and left even tho im only holding left now, till i release left and press again, then it just goes left.
Not sure which scripts I should post so I'll post all but only the parts of them that involve input of the up key, to keep it short. The left, right, down, scripts are the same except they control different variables.
JoyUp.js
#pragma strict
guiTexture.enabled = false;
var menuOptionsScript : MenuOptions;
var menuOptions : GameObject;
function Update ()
{
if(Input.touchCount > 0 ){
for(var i : int = 0; i < Input.touchCount; i++)
{
var touch : Touch = Input.GetTouch(i);
if(touch.phase == TouchPhase.Began && guiTexture.HitTest(touch.position))
{
menuOptionsScript = menuOptions.gameObject.FindWithTag("MenuOptions").GetComponent(MenuOptions);
menuOptionsScript.upClicked = true;
}
}
}
else
{
menuOptionsScript = menuOptions.gameObject.FindWithTag("MenuOptions").GetComponent(MenuOptions);
menuOptionsScript.upClicked = false;
}
}
MenuOptions.js
var androidToggle = false;
var upClicked : boolean = false;
var downClicked : boolean = false;
var leftClicked : boolean = false;
var rightClicked : boolean = false;
function Awake()
{
DontDestroyOnLoad (transform.gameObject);
}
function OnGUI ()
{
androidToggle = GUI.Toggle (Rect (25, 50, 120, 30), androidToggle, "Android Controls");
if(androidToggle == true) //show/hide android controls
{
gameObject.Find("JoyUp").guiTexture.enabled = true;
gameObject.Find("JoyDown").guiTexture.enabled = true;
gameObject.Find("JoyLeft").guiTexture.enabled = true;
gameObject.Find("JoyRight").guiTexture.enabled = true;
}
else if(androidToggle == false)
{
gameObject.Find("JoyUp").guiTexture.enabled = false;
gameObject.Find("JoyDown").guiTexture.enabled = false;
gameObject.Find("JoyLeft").guiTexture.enabled = false;
gameObject.Find("JoyRight").guiTexture.enabled = false;
}
}
PlayerController.js
var mainThruster : ParticleEmitter;
var haveFuel : boolean;
var mainThrusterSpeed : int = 10;
var menuOptionsScript : MenuOptions;
var menuOptions : GameObject;
var GUI : InGameGUI;
function Start ()
{
GUI = GameObject.FindWithTag("GUI").GetComponent(InGameGUI);
menuOptionsScript = menuOptions.gameObject.FindWithTag("MenuOptions").GetComponent(MenuOptions);
}
function FixedUpdate ()
{
if(haveFuel)
{
if(Input.GetAxis("Vertical") > 0 || menuOptionsScript.upClicked) //Checking for up arrow key or touch
{
mainThruster.emit = true;
rigidbody.AddForce(0,mainThrusterSpeed,0);
}
if(Input.GetAxis("Vertical") == 0 && menuOptionsScript.upClicked==false) //Checking if no vertical keys are down
{
mainThruster.emit = false;
}
}
}
Again, I only pasted the parts of the scripts related to the GUI texture up button and controls, so its less crowded. If you reached this far, thank you for reading at least. xD
Answer by robertbu · Jul 30, 2013 at 02:42 AM
Your problem is rooted in line 10. That is, once you've turned your Thruster on, it only get turned off if Input.touchCount is zero. And it will not be zero if another button is held down. You can fix your code by restructuring it to (untested):
#pragma strict
guiTexture.enabled = false;
var menuOptionsScript : MenuOptions;
var menuOptions : GameObject;
function Start() {
menuOptionsScript = menuOptions.gameObject.FindWithTag("MenuOptions").GetComponent(MenuOptions);
}
function Update ()
{
menuOptionsScript.upClicked = false;
for(var i : int = 0; i < Input.touchCount; i++)
{
var touch : Touch = Input.GetTouch(i);
if(touch.phase == TouchPhase.Began && guiTexture.HitTest(touch.position))
{
menuOptionsScript.upClicked = true;
}
}
}
Note I made one other change as well. I move the initialization of menuOptionsScript into Start(). In fact you don't need to do that at all. If you drag and drop the same object you dragged onto 'menuOptions' onto 'menuOptionsScript' in the Inspector, Unity will establish the link for you.
Thanks for answering! Altho, now the upClicked is only true the moment I touch the button so I have to click it at incredible speeds to have any effect. xD
Should I use stationary ins$$anonymous$$d of began? Or is it something else entirely? :S
Yup changing it to stationary worked :D Thank you so much you're my savior. xD
Now the game is ready for testing when I optimize it for the phone. Right now its only for PC with a lot of high res textures and particles and runs fine on my phone but my phone is 1ghz dual core, I need it optimized for 600mhz phones. xD
Your answer
Follow this Question
Related Questions
Touch Input with GUI Texture 1 Answer
How would I implement a GUI texture that acts as a slider? 0 Answers
Drag and Drop button on Mobile (Messenger style) 0 Answers
Touch Input on Sprites 1 Answer
GUITexture HitTest not registering 0 Answers