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;

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.