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
1
Question by Guzaii · Nov 29, 2013 at 06:48 AM · 2dscipting

Multi Touch GUI buttons

I got this script, i tested it with my tablet, and it worked. But how will i be able to press 2 GUI.Buttons at the same time? (like run and jump at same time)

Here's what i got:

     public Texture2D leftIcon;
     public Texture2D rightIcon;
     public Texture2D jumpIcon;
 
     public GUIStyle framestyle;
 
     void OnGUI () {
 
 
         if(GUI.RepeatButton(new Rect(200,Screen.height - 200,200,200), rightIcon, framestyle)){
             float h = 1;
             if(h * rigidbody2D.velocity.x < maxSpeed){                
                 // ... add a force to the player.
                 rigidbody2D.AddForce(Vector2.right * h * moveForce);
             }
             //Debug.Log ("velocity.x :" + rigidbody2D.velocity.x);
             
             // If the player's horizontal velocity is greater than the maxSpeed...
             if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)
                 // ... set the player's velocity to the maxSpeed in the x axis.
                 rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);
             
             // If the input is moving the player right and the player is facing left...
             if(h > 0 && !facingRight)
                 // ... flip the player.
                 Flip();
             // Otherwise if the input is moving the player left and the player is facing right...
             else if(h < 0 && facingRight)
                 // ... flip the player.
                 Flip();
         }
 
         if(GUI.RepeatButton(new Rect (0,Screen.height - 200,200,200), leftIcon, framestyle)) {
             float h = -1;
             if(h * rigidbody2D.velocity.x < maxSpeed){                
                 // ... add a force to the player.
                 rigidbody2D.AddForce(Vector2.right * h * moveForce);
             }
             //Debug.Log ("velocity.x :" + rigidbody2D.velocity.x);
             
             // If the player's horizontal velocity is greater than the maxSpeed...
             if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)
                 // ... set the player's velocity to the maxSpeed in the x axis.
                 rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);
             
             // If the input is moving the player right and the player is facing left...
             if(h > 0 && !facingRight)
                 // ... flip the player.
                 Flip();
             // Otherwise if the input is moving the player left and the player is facing right...
             else if(h < 0 && facingRight)
                 // ... flip the player.
                 Flip();
         }
 
         if(GUI.Button (new Rect(Screen.width - 200,Screen.height - 200,200,200), jumpIcon, framestyle) && grounded){
             jump=true;
         }
     }
 }


(Its a change of the Unity2D controller script)

Comment
Add comment · Show 1
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 THELORDOFDESTRUCTION · Jul 06, 2016 at 03:36 AM 0
Share

"(Its a change of the Unity2D controller script)"

Can you tell me where exatly is that script, I'm trying to find it but I don't know where is it :(

Thank you :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by fafase · Nov 29, 2013 at 07:48 AM

You would have to use a different approach.

Each button is within a rectangle, you already have them you just need to store them in a Rect, so you can check for Contains.

 Rect rightIconRect = new new Rect(200,Screen.height - 200,200,200);
 Rect leftIconRect = new Rect (0,Screen.height - 200,200,200);
 foreach(Touch t in Input.touches)
 {
     Vector2 vec = t.position;
     vec.y = Screen.height - vec.y; // You need to invert since GUI and screen have differnet coordinate system
     if(rightIconRect.Contains(vec))// Do something
     if(leftIconRect.Contains(vec)) // Do something
 }

Also, I think some touches are actually really close, it could return two touches from the same finger, so you may want to make sure you are calling performing twice an action from the same finger just because you have multiple touches from it.

Comment
Add comment · Show 5 · 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 Guzaii · Dec 02, 2013 at 09:25 AM 0
Share

I have absolute no clue where to place this.. But thanks for the help :)

avatar image fafase · Dec 03, 2013 at 08:07 AM 0
Share

That can go in your Update.

avatar image Srbhunter · Mar 07, 2014 at 07:20 PM 0
Share

Thank you for your answer, it helped me a lot!

avatar image anubhav756 · Mar 25, 2014 at 06:12 PM 0
Share

O$$anonymous$$....how to add texture (or at least text) to the rect that we have created....??

avatar image fafase · Mar 27, 2014 at 05:38 AM 0
Share

Use some GUITexture. Good point, it is more efficient than GUI.

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

20 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Increase Jump Through Mouse Clicks? 1 Answer

A node in a childnode? 1 Answer

Switching Cameras at runtime 1 Answer

2D Game - Y axis rotation (left - right) 2 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