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 cubelord · Dec 25, 2011 at 04:46 AM · guiiphoneairplane

controll acceleration with gui button

i have a script to control my airplan and i want to make it work with the iphone so i want to use gui buttons to accelerate i have the accelerationg script here how do i make it work with gui buttons

 //---------------------------- Speed ​​driving and flying / driving speed and flying ----------- -------------------------------------------------- ---
 
 / / Speed ​​| | Speed
 transform.Translate (0.0, speed/20 Time.deltaTime *);
 
 / / We need a minimum speed limit in the air. We again limit the variable groundtrigger.triggered
 / / We need a minimum speed limit in the air. We limit again with the variable groundtrigger.triggered
 
 / / Input to accelerate and decelerate on the ground | | and input Accellerate deccellerate at ground
 if ((groundtrigger.triggered == 1) & & (Input.GetButton ("Fire1 "))&&( speed <800)) + speed = Time.deltaTime * 240;
 if ((groundtrigger.triggered == 1) & & (Input.GetButton ("Fire2 "))&&( speed> 0)) speed = Time.deltaTime * 240;
 
 / / Input to accelerate and decelerate in the air | | and input Accellerate deccellerate in the Air
 if ((groundtrigger.triggered == 0) & & (Input.GetButton ("Fire1 "))&&( speed <800)) + speed = Time.deltaTime * 240;
 if ((groundtrigger.triggered == 0) & & (Input.GetButton ("Fire2 "))&&( speed> 600)) speed = Time.deltaTime * 240;
 
 if (speed <0) speed = 0, / / ​​floating point calculations makes a fix Necessary So that speed can not be below zero
 / / Floating point calculation makes a fix may be necessary to speed not less than zero
 
 / / Another speed floating point fix:
 if speed = 700;
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
3
Best Answer

Answer by Rabwin · Dec 25, 2011 at 05:59 AM

Check here for information about GUI Buttons

Following code is in C#, not JavaScript! Look at the link for JavaScript examples.

Basically you need a OnGui function which is like an update function but is specifically used to handle GUI elements in a 2D environment. GUI Elements will always render on top of scene elements.

When creating a button, you need to setup the x and y position, and the width and height of the button.

 Rect MyButton = new Rect(10, 10, 100, 30); // x, y, width, height
 if(GUI.Button(MyButton,"My Button") // and this is your if statement to create the button for a single action
 OR
 if(GUI.Button(new Rect(10, 10, 100, 30),"My Button") // this does the same without creating a variable for the button, if you didn't need to cache that information. I like to cache it so I can use it on arrays of buttons.

When creating a button, you can do tricks like scaling it to a portion of the screen size so that it will always be a particular size to the screen, useful for porting to multiple devices who have different screen size. Example:

 Rect ResizingButton = new Rect(screen.width*0.1, screen.height*0.1, screen width*0.3, screen height*0.15);

Heres some example code to get you started (Untested code in C#, check the link for a javascript version): Optimising your code a little, this is for acceleration. Write another "if(GUI.Button" for deceleration.

 void OnGUI()
 {
     if(GUI.Button(new Rect(10, 10, 100, 50),"Accelerate"))
     {
         if ((groundtrigger.triggered == 1) (Input.GetButton ("Fire1 "))&&( speed <800)) {speed += Time.deltaTime * 240;}
         else if ((groundtrigger.triggered == 1) (Input.GetButton ("Fire1 "))&&( speed <800)) {speed += Time.deltaTime * 240;}
     }
 }

Leave a comment and tell me if you're having trouble or if my answer didn't suit your needs.

Comment
Add comment · Show 6 · 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 cubelord · Dec 25, 2011 at 08:25 AM 0
Share

how do i make it on tap because i want this to work on the iphone

avatar image cubelord · Dec 25, 2011 at 09:04 AM 0
Share

and it gives me a few errors like expression denotes a value where a method is expected here is the final code

using UnityEngine; using System.Collections;

public class accelerate : $$anonymous$$onoBehaviour {

 void OnGUI()

{ if(GUI.Button(new Rect(10, 10, 50, 50),"Accelerate")) { if ((groundtrigger.triggered == 1) (Input.GetButton ("Fire1 "))&&( speed <800)) + speed = Time.deltaTime 240; else if ((groundtrigger.triggered == 0) (Input.GetButton ("Fire1 "))&&( speed <800)) + speed = Time.deltaTime 240; } } }

avatar image Rabwin · Dec 25, 2011 at 12:54 PM 0
Share

Unity codes GUI.Button to work for touches, but without using the code for handling multiple touches I think that won't work. You can check for multi-touch with this method (There are probably other ways but this is easiest):

Rect buttonArea = new Rect(10,10,100,50); foreach ( Touch touch in Input.touches) { if (touch.phase == TouchPhase.Began && buttonArea.Contains(touch.position)) { //Do stuff } }

I copy pasted the code directly from your question, try changing the if statements to this:

if ((groundtrigger.triggered == 1) (Input.GetButton ("Fire1 "))&&( speed <800)) {speed += Time.deltaTime * 240;}

I have also edited my answer in reply to this error. You may also want to change the values of acceleration whether on the ground or not, since they're the same in your quesiton or maybe you left it like that intentionally, just making sure.

avatar image cubelord · Dec 26, 2011 at 01:10 AM 0
Share

ok now it gives me more errors when i put it in it tells me more errors can you put it into your editor and tell me where im going wrong cause this is the final code i get here

using UnityEngine; using System.Collections;

public class speed : $$anonymous$$onoBehaviour {

 void OnGUI() {
     
 if(GUI.Button(new Rect(10, 10, 100, 50),"Accelerate"))
 {
     if ((groundtrigger.triggered == 1) (Input.GetButton ("Fire1 "))&&( speed <800)) {speed += Time.deltaTime * 240;}
     else if ((groundtrigger.triggered == 1) (Input.GetButton ("Fire1 "))&&( speed <800)) {speed += Time.deltaTime * 240;}
 }

     
 }

}

avatar image cubelord · Dec 26, 2011 at 06:24 AM 0
Share

can i send you my project through email and have u set up the controls like left right accelerate and i can put you down as co developer needed a partner anyway

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

I have problem when i want to using button instead keyboard in iOS 1 Answer

Make this GUI Bigger? 1 Answer

GetComponent Help 2 Answers

swing to hit a target 1 Answer

iPhone GUI buttons problem 1 Answer


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