Search Unity

help with GUI "Boost" button...

Discussion in 'Immediate Mode GUI (IMGUI)' started by etomp10291, Oct 18, 2010.

  1. etomp10291

    etomp10291

    Joined:
    Jan 11, 2010
    Posts:
    108
    i am working on a arcade flight sim game for iPhone. I need to create a "boost" button that would boost speed of plane as button is pressed and return to normal as depressed (obviously). I have studied the script reference and gui reference but cannot get my head around it. here is button script...

    function OnGUI () {
    if (GUI.RepeatButton (Rect (Screen.width - 100,Screen.height - 50,100,50), "Boost!")) {
    // This code is executed every frame that the RepeatButton remains clicked

    }
    }

    i need that button script to increase this script below...

    thisRigidbody.AddForceAtPosition( thisTransform.forward * 1000, forceThrust.position );

    need to increase the "1000" integer when button is pressed...i do not have a throttle control, plane flies at constant speed when game starts, just want to add a little speed boost to help game play.

    thanks for the assistance :)
     
  2. laurie

    laurie

    Joined:
    Aug 31, 2009
    Posts:
    638
    Code (csharp):
    1.  
    2. private var isBoosted : boolean = false;
    3. private var normalSpeed : int = 1000;
    4. private var boostSpeed : int = 2000;
    5.  
    6. function OnGUI() {
    7.   isBoosted = false;
    8.   if (GUI.Button(Rect(...), "Boost!") {
    9.     isBoosted = true;
    10.   }
    11. }
    12.  
    Then, in the Update() / FixedUpdate() / where ever you apply your forces,

    Code (csharp):
    1.  
    2. var speed : int;
    3. if (isBoosted) {
    4.   speed = boostSpeed;
    5. } else {
    6.   speed = normalSpeed;
    7. }
    8. thisRigidbody.AddForceAtPosition( thisTransform.forward * speed, forceThrust.position );
    9.  
     
  3. etomp10291

    etomp10291

    Joined:
    Jan 11, 2010
    Posts:
    108
    thank you!!!
     
  4. etomp10291

    etomp10291

    Joined:
    Jan 11, 2010
    Posts:
    108

    thanks for help, still having issues, i sent you a PM.