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
0
Question by CallToAdventure · Nov 27, 2010 at 01:03 PM · inputbuttondisableenable

This should be simple : Input Button Question

Hi everyone

So this is what I try to do: enable / disable the movement of my plane; I setup 2 input buttons for this and proceeded next;

var EngineOn = false; var EngineOff = false;

function FixedUpdate () {

if(Input.GetButton("StartEngine")) { EngineOn = true; EngineOff = false;

rigidbody.AddRelativeForce(0, 0, normalizedSpeed * forwardForce); //.. and so on }

if(Input.GetButton("StopEngine"))

{ EngineOn = false; EngineOff= true;

rigidbody.AddRelativeForce(0, 0, 0); //.. and so on

}

Currently this works just fine with a twist - unless I keep the button pressed, the command turns off.

I don't know how offensive that is to decent programmers out there but this is how an artist sees the code :) What I try to do is enable the bit of code that does all the acrobatics with my gameObject and simply disable that bit of code when the engine is not running; So, in how many ways I am wrong with this approach?

Thanks in advance,

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
0

Answer by Mike 3 · Nov 27, 2010 at 01:10 PM

Use Input.GetButtonDown instead, otherwise it'll fire your conditional statements every frame the button is held down

After that, you'll want to Add the force in FixedUpdate instead of where you have it, e.g:

var EngineOn = false; var EngineOff = false;

function FixedUpdate () { if (EngineOn) rigidbody.AddRelativeForce(0, 0, normalizedSpeed * forwardForce); }

function Update() {

 if(Input.GetButton("StartEngine"))
 {
     EngineOn = true;
     EngineOff = false;

     //.. and so on
 }

 if(Input.GetButton("StopEngine"))

 {
     EngineOn = false;
     EngineOff= true;
 }

}

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 CallToAdventure · Nov 27, 2010 at 01:14 PM 0
Share

I tried that first hand but it doesn't work.

avatar image Mike 3 · Nov 27, 2010 at 01:15 PM 0
Share

Heh, I hit post too early and my internet died before I could fix it. Anyways points up at a nicer answer

avatar image CallToAdventure · Nov 27, 2010 at 01:15 PM 0
Share

This happens in FixedUpdate function maybe it should be moved to Update?

avatar image CallToAdventure · Nov 27, 2010 at 01:19 PM 0
Share

I love it when I learn, thank you $$anonymous$$ike.

avatar image Mike 3 · Nov 27, 2010 at 02:20 PM 0
Share

Update is much better for the input checking where you check if a key was pressed (doesn't matter too much about when you're checking if it's held), FixedUpdate is better for adding forces

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

No one has followed this question yet.

Related Questions

how to disable a button after pressing it and enable again after pressing another button 1 Answer

Disable, Enable a User Input? 1 Answer

Enable/Disable GameObject Button script 1 Answer

Can`t regulary send bool value from one script to another after disabling gameobject and enabling again 1 Answer

How to disable collision detection while a UI Panel is enabled 2 Answers


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