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 /
  • Help Room /
avatar image
0
Question by pkwe · Oct 10, 2015 at 01:12 PM · if-elseif else

If Else problem

Hello, i'm new to this unity. so i have this code, when i press enter once, it always return true first then quickly return false. is this common thing? or is there something wrong in my code?

 public float maxSpeed;
 float accel = 0;
 int check;
 
 // Use this for initialization
 void Start () {
     accel = maxSpeed / 5;
 }
     
 // Update is called once per frame
 void Update () {
     if (Input.GetKeyDown (KeyCode.Return) && check == 0) {
         check = 1;
     } else {
         check = 0;
     }
         
     if (check == 1) {
         transform.Rotate (Vector3.forward, maxSpeed * Time.deltaTime);
     }
         
     Debug.Log ("check = " + check);
 }
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
1
Best Answer

Answer by Dave-Carlile · Oct 10, 2015 at 01:15 PM

According to the GetKeyDown documentation, the function returns true only on the frame when the key was pressed. It won't return true again until the key is released and then pressed again, so that is the correct behavior. This would be used when you only want something to happen once on a keypress and not repeat over and over each frame.

If you want to check the actual state each frame then you'd use Input.GetKey instead which always gives you the current up/down state of the key.

I'd suggest taking a look at this tutorial video: https://unity3d.com/learn/tutorials/modules/beginner/scripting/get-button-and-get-key

Comment
Add comment · Show 3 · 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 pkwe · Oct 10, 2015 at 04:58 PM 0
Share

yeah you right. so what the best method to use for if else beside getkey and getbutton?? i already seen that video. but no. i want a single button that if press one then return true and then if press again then return false.

avatar image Dave-Carlile pkwe · Oct 10, 2015 at 07:26 PM 0
Share

Okay, I wasn't understanding what you wanted... So you want the object to start out stationary, and when you press the button it starts rotating and will keep rotating until you press the button again?

You're close in that you're trying to toggle a value back and forth between 0 and 1. But you don't need an else at all..

 bool rotate;

 void Update()
 {
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Return))
     {
         rotate = !rotate;
     }

     if (rotate)
     {
         transform.Rotate (Vector3.forward, maxSpeed * Time.deltaTime);
     }
 }


! is the not operator. It takes a boolean value and returns the opposite. So !true = false and !false = true. Booleans are a great way to toggle a value back and forth. So in my example, each time the Return key is pressed it will toggle the value of rotate to the opposite of whatever it is now.

Then the rotate code is put inside an if so it only executes when rotate is true.

avatar image pkwe Dave-Carlile · Oct 13, 2015 at 02:23 AM 0
Share

thank you. this is what i'm looking for. i didnt know if you can do like that.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to put an if statement inside an if statement? 3 Answers

If/else statement is always else 1 Answer

How to use IF in Unity for selection MyScript 1 Answer

Simple toggle not working 1 Answer

How to turn on and off a bool with a button press 3 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