x


Has anyone got a Toggle key script?

Hi, I've spent countless hours trying to create a Toggle key script, I've even improved my programming skills alot from it, but I still cant create that toggle script, even when I look at other scripts with the "Toggle" element. Has anyone got a script that'll alow me to toggle a key? For instance..

I've been trying to turn this into a toggle code, if (Input.GetButton("Fire1")){ I've deleted my previous attempts so i cant show you what I've been doing sadly :( But that's what I've been working from. I'm trying to make an Object follow me when I press the "Fire1" key, In this case the LeftMouseButton.

Here's my entire code.

var targetTransform : Transform;       // Transform to follow
var faceForward : boolean = false;   // Match forward vector?
private var thisTransform : Transform;

function Start()
{

    // Cache component lookup at startup instead of doing this every frame
    thisTransform = transform;
}



function Update   () 
{


if (Input.GetButton("Fire1")){
     print ("Activated");

     thisTransform.position = targetTransform.position;

    if ( faceForward )
       thisTransform.forward = targetTransform.forward;


        else {
        print ("NotActivated");



     }

     }

     }

Dave A here's your Version, which isn't work either.

var targetTransform : Transform; // Transform to follow private var thisTransform : Transform; var Active : boolean;

function Start() {

// Cache component lookup at startup instead of doing this every frame
thisTransform = transform;

}

function Update () {

     if (Input.GetButton("Fire1")){
     Active = !Active; // toggle this value
     if ( Active )
     thisTransform.position = targetTransform.position;
     print ("Active");
 }
    else {


    Active = Active; // toggle this value
     if ( Active )
     print ("NotActive");
     }

} The Oddler here's your suggestion.

var targetTransform : Transform;       // Transform to follow       
private var thisTransform : Transform;
var Active : boolean;

function Start()
{

    // Cache component lookup at startup instead of doing this every frame
    thisTransform = transform;
}



function Update   () {


         if (Input.GetButtonUp("Fire1")){
         Active = !Active; // toggle this value
         if ( Active ) {
         thisTransform.position = targetTransform.position;

         print ("Active");
     }
        else {


        Active = Active; // toggle this value
         if ( Active ) {
         print ("NotActive");
         }

        }
}}

Can anyone help me out please? The help would be greatly appreciated. Thanks for reading.

more ▼

asked Sep 03 '11 at 01:26 AM

Dryden Richardson gravatar image

Dryden Richardson
135 37 45 47

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

I'm a bit tired, so I can't be sure if this is what you're looking for, but I recently finished this code that makes a certain variable true when pressed, and then false when pressed again:

function Update () {
    if(Input.GetButtonDown("Fire1") && !Active)
       Activate();

    if(Active && Input.GetButtonUp("Fire1"))
       Active = false;

}

function Activate () {
    yield WaitForSeconds(.1);
    Active = true;

}
more ▼

answered Sep 03 '11 at 10:44 PM

McMutton gravatar image

McMutton
121 27 31 38

Thanks! I think it works too.

Sep 04 '11 at 12:03 AM Dryden Richardson
(comments are locked)
10|3000 characters needed characters left
faceForware = !faceForward; // toggle this value
if ( faceForward )
more ▼

answered Sep 03 '11 at 01:51 AM

DaveA gravatar image

DaveA
26.5k 151 171 256

Still not working :(

Sep 03 '11 at 04:35 PM Dryden Richardson

Might be because it fires twice, once for the button down and once for up. Might want to use something like Inpuy.GetButtonUp("Fire"); (check for the exact method, not sure it's called that :P)

Sep 03 '11 at 04:39 PM The Oddler

Baffled :P

Sep 03 '11 at 04:45 PM Dryden Richardson

Your code: 'if (Input.GetButton("Fire1"))' change that to: 'if (Input.GetButtonUp("Fire1"))'. This will only fire when the button goes up while 'GetButton' continuously fires, which you don't want for a toggle.

So entire code would become:

if (Input.GetButtonUp("Fire1")) { faceForware = !faceForward; // toggle this value }

if ( faceForward ) { //Do stuff }

Look at: http://unity3d.com/support/documentation/ScriptReference/Input.GetButtonUp.html

Sep 03 '11 at 04:57 PM The Oddler

Thanks for trying to help, But still Simmilar results. Check the Edit.

Sep 03 '11 at 09:21 PM Dryden Richardson
(comments are locked)
10|3000 characters needed characters left

If you want to perform an action only when the button is pressed, replace the print functions to your own functions.

var toggle : Boolean;

function Update() {
    if (Input.GetButtonDown("Fire1")) {
        toggle = !toggle;

        if (toggle) 
            print("Toggled ON");
        else
            print("Toggled OFF");
    }    
}

If you want to perform an action every update depending on the toggle value, just move the switching logic outside the input test, as shown here:

var toggle : Boolean;

function Update() {
    if (Input.GetButtonDown("Fire1"))
        toggle = !toggle;

    if (toggle) 
        print("Toggled ON"); 
    else
        print("Toggled OFF");
}
more ▼

answered Sep 03 '11 at 09:35 PM

Statement gravatar image

Statement ♦♦
20.1k 35 70 175

That's what I said in a little more words, should work perfectly !

Sep 03 '11 at 09:46 PM The Oddler

Thank you Statement and The Oddler you helped me sort out this problem. @Statement Thank you, the bottom did the trick :).

Sep 03 '11 at 11:00 PM Dryden Richardson
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3460
x1952
x159

asked: Sep 03 '11 at 01:26 AM

Seen: 2274 times

Last Updated: Sep 07 '11 at 05:55 PM