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 Warlyte · Feb 20, 2014 at 02:49 AM · best practice

Well coded or just silly?

Okay, this is a pretty low-priority question, but for learning's sake, I wanted to know. The code below works fine for what I wanted to do, but frankly with all the conditions going every which way I have no idea if this was best practice. Is there an easier or more efficient way to do the same thing?

         //Pressing the button when ready makes the player jump,
         //and disables the ability to jump again.
         if(jumpEnabled && Input.GetButtonDown("Jump")){
             _myMovementScript.jumping = true;
             jumpEnabled = false;
         }
         //The same button pressed again can start flying behavior.
         else if (flightEnabled && Input.GetButton("Jump")){
             _myMovementScript.flying = true;
         }
         //If the player releases the button they will be neither flying nor jumping.
         //Flight is only enabled after releasing the button the first time.
         else if(!Input.GetButton("Jump") && !_myMovementScript.onGround)
         {
             flightEnabled = true;
             _myMovementScript.flying = false;
             _myMovementScript.jumping = false;
         }
         //The player can only jump again from the ground.
         //It enables only when the button is released to avoid holding the button and jumping continuously.
         else if(!Input.GetButton("Jump") && _myMovementScript.onGround){
             _myMovementScript.jumping = false;
             _myMovementScript.flying = false;
             jumpEnabled = true;
             flightEnabled = false;
         }
         //Handles the player still holding the button when neither action is enabled.
         else
         {
             _myMovementScript.jumping = false;
             _myMovementScript.flying = false;
         }
Comment
Add comment · Show 3
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 getyour411 · Feb 20, 2014 at 02:57 AM 0
Share

There's a couple places where your comments say 'only when button is released' but your condition is just GetButton ins$$anonymous$$d of GetButtonUp. Otherwise, the code that works best is code you can maintain (and works of course). I'd say leave it and move on, you don't want to change it to 'advanced & abstracted' code you get from someone else that you can't maintain or modify.

avatar image mattyman174 · Feb 20, 2014 at 03:02 AM 1
Share

When learning, the best code in my opinion is code you write yourself. To many times to people copy and paste code and then have no idea how it works.

If you have written you own code and it works, regardless of best practice of efficiency shows that you understand the concept and a loose flow of what your trying to achieve looks like.

After you fully understand what you have written can you come back and try and push your boundaries of knowledge and think of better ways to re-write something.

avatar image whydoidoit · Feb 20, 2014 at 04:13 AM 1
Share

You are creating a statemachine using if statements, which is fine but does tend to spiral out of control. If you think about states then you can make this kind of thing more efficient and readable.

States would require you branching earlier on the current mode of the character and then testing the input valid in that state.

For instance you could use a switch based on the current mode (like idle, walking, running, jumping, attacking, falling or dying). Within each of those modes you would test your input and make any other specific changes. more advanced techniques would include using delegates or inheritance to have different code run in each state and you might consider the code you run as states are exited and entered (transitions).

0 Replies

· Add your reply
  • Sort: 

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

21 People are following this question.

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

Related Questions

How will we enable and disable a gui texture in unity 3d? 5 Answers

Unity Networking, connecting to sever problems :( 1 Answer

Detecting another author's lib/package/classes 1 Answer

For Loop Won't Stop Running... 1 Answer

Character Code like Minecraft? 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