Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Dryden Richardson · Sep 03, 2011 at 01:26 AM · javascript

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.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Statement · Sep 03, 2011 at 09:35 PM

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");
 }
Comment
Add comment · Show 2 · 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 The-Oddler · Sep 03, 2011 at 09:46 PM 0
Share

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

avatar image Dryden Richardson · Sep 03, 2011 at 11:00 PM 0
Share

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

avatar image
1

Answer by DaveA · Sep 03, 2011 at 01:51 AM

 faceForware = !faceForward; // toggle this value
 if ( faceForward )
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 Dryden Richardson · Sep 03, 2011 at 04:35 PM 0
Share

Still not working :(

avatar image The-Oddler · Sep 03, 2011 at 04:39 PM 0
Share

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

avatar image Dryden Richardson · Sep 03, 2011 at 04:45 PM 0
Share

Baffled :P

avatar image The-Oddler · Sep 03, 2011 at 04:57 PM 0
Share

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

avatar image Dryden Richardson · Sep 03, 2011 at 09:21 PM 0
Share

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

Show more comments
avatar image
1

Answer by McMutton · Sep 03, 2011 at 10:44 PM

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;
 
 }
Comment
Add comment · Show 1 · 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 Dryden Richardson · Sep 04, 2011 at 12:03 AM 0
Share

Thanks! I think it works too.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Changing material Color using RBG? 3 Answers

Int and Javascript help 2 Answers

Integer in if statement, scripting help 1 Answer

Child Obj Ignoring Parents Rotation! Help 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