Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ChrisSch · Jul 30, 2013 at 12:38 AM · guitextureinputtouchguitexture

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ChrisSch · Jul 30, 2013 at 08:27 AM 0
Share

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

avatar image ChrisSch · Jul 30, 2013 at 08:46 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges