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 sarim987 · Feb 18, 2014 at 04:18 AM · guimobiletouchcarcontrol

Adding Touch Controls to Car Game

hello. Ive spent a couple hours looking on the forums and i am just really confused and dont know how to properly handle this. I have made 2 buttons. A gas pedal (gas) and a brake pedal (brake). I have added them to my gui. I am having trouble adding these buttons to the code and making it so when the gas is pressed or held, the car goes. and when the brake is pressed or held, it starts stopping.

Here is the car game movement code

http://pastebin.com/YV1RmH6J

Any help would be appreciated.

Comment
Add comment · Show 7
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 getyour411 · Feb 18, 2014 at 04:22 AM 1
Share
 OnGUI{
 
 if(GUI.Button(Rect()),Gas) {
 // gas function
 }
 
 if(GUI.Button(Rect()),Brake) {
 // break function
 }
 }

I didn't look at your code but generally if you are using a GUI button it's something like that

avatar image sarim987 · Feb 18, 2014 at 04:37 AM 0
Share

Does this include the finger touching the button?

avatar image getyour411 · Feb 18, 2014 at 04:45 AM 0
Share

I don't do mobile but I'm assu$$anonymous$$g the if(GUI.Button...) struct works the same - test it and see or search. Are you using OnGUI or are you using something like if(Input.GetAxis($$anonymous$$eyCode.*))?

avatar image sarim987 · Feb 18, 2014 at 12:43 PM 0
Share

Forgot to say I am using a c# script and will need mul$$anonymous$$ch on my game. I Did some research and don't think this works with mul$$anonymous$$ch. How would you do the if(Input.GetAxis($$anonymous$$eyCode)) code? I'm having trouble implementing that into my code. Thanks for all your help!!

avatar image sarim987 · Feb 18, 2014 at 04:40 PM 0
Share

What is the best way to approach this? I am still very confused despite all the research.

Show more comments

1 Reply

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

Answer by POLYGAMe · Feb 18, 2014 at 08:55 PM

Here's how I did it in my game Get Gravel (these are GUITexture buttons):

 for (var touch : Touch in Input.touches) 
     {
         if (GasButton.HitTest(touch.position))
         {
             bBrakeButtonPressed = false;
             bGasButtonPressed = true;
         }
         else if (BrakeButton.HitTest(touch.position))
         {
             bGasButtonPressed = false;
             bBrakeButtonPressed = true;
         }
         
         if (LeftButton.HitTest(touch.position))
         {
             bRightButtonPressed = false;
             bLeftButtonPressed = true;
         }
         else if (RightButton.HitTest(touch.position))
         {
             bLeftButtonPressed = false;
             bRightButtonPressed = true;
         }
     }


Then just use those bools to control your car.

Comment
Add comment · Show 13 · 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 POLYGAMe · Feb 18, 2014 at 09:00 PM 0
Share

Ignore the dodgy formatting... I think it's cos I cut the code from TextEdit.

avatar image sarim987 · Feb 18, 2014 at 09:03 PM 0
Share

Hi thanks for answering. Does this support mul$$anonymous$$ch? for example can I hold the gas down and the left button at the same time? And is this c#? Thank you!!!

avatar image POLYGAMe · Feb 18, 2014 at 09:06 PM 1
Share

Yup, supports multi touch. It counts through and detects each touch. This is in JS but the code should be pretty much identical, other than the for loop, which from looking at the docs would be:

  foreach (Touch touch in Input.touches) 

Don't forget to accept the answer as correct if this helps :)

avatar image sarim987 · Feb 18, 2014 at 09:18 PM 0
Share

When it says bBrakeButtonPressed do I just replace it with brakepressed because my button is named brake?

avatar image POLYGAMe · Feb 18, 2014 at 09:35 PM 0
Share

Those are just booleans I set up for my game. You can do whatever you want in there when the button is pressed. Also, you'll have to swap out the names of the buttons for the names of your GUITexture buttons.

Show more comments

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

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

Related Questions

How to make camera position relative to a specific target. 1 Answer

Drag and Drop button on Mobile (Messenger style) 0 Answers

Touch not working on guiTexture. 1 Answer

How to make GUI pop up when you touch other gui 1 Answer

Why this simple code doesnt work? 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