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 Kiloblargh · Nov 06, 2012 at 04:25 PM · characterorganizationbest-practices

How do you organize a character control script to avoid spaghetti code?

I just started on a player control script, and with less than half of the functionality I want the character to have, it's already an incomprehensible mess. (it unfortunately resembles the desktop on my computer and the actual top of my desk.) I was adding one thing at a time and now have a ton of variables and my Update() is pages of if / else if /else statements with multiple &&s and ||s in the ifs. So now I'm having issues where it starts spazzing out and getting stuck in loops of jumping and it's hard to track down why.

I need to start over and rewrite it with care, but not sure how to do things differently so I don't get back to the same problems. Do I split up everything into little functions, and only call functions from Update? Should I have several scripts attached to the player; one to just handle walking, one for jumping, one for climbing, one for throwing things, etc?

I know this is more a general programming problem than a Unity problem, but one that probably everyone who is self-taught in scripting and first takes on more than an extremely simple game is faced with.

How did you solve it, and/or what should I go read?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Yokimato · Nov 06, 2012 at 04:36 PM

Think modular!

What I have is an Update method that will call a number of simple, small, and most importantly easy to ready methods. One to gather input, one to handle moving, one to handle actions, etc. Sometimes you're just not going to get away from a lengthy method (Ones with switch statements, for example), and it's important that you don't break it up for length alone...however it's good to refactor your code often to ask yourself "Does this belong here?" and "Does this action deserve it's own method?". More often then not, you can keep your code clean by simply going back and re-reading what you just wrote. If you google some refactoring methods, you could get some more indepth ideas, but overall, my vote is to think small, think modular, and be your own code review in lieu of others. Good luck!

Comment
Add comment · 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
2

Answer by MibZ · Nov 06, 2012 at 04:43 PM

When I was first being taught how to program, the only thing we were allowed to have inside the Update function were function calls because that makes it MUCH easier to debug your program.

That said, when I was first learning how to program the most complicated game I programmed was Tic-Tac-Toe. (More complicated than you would think, though!)

I wouldn't say that you need to only have function calls in Update, but I would recommend making good use of regions - they can be VERY helpful for organizing your code into readable blocks, but you need to be as careful setting up regions as you are setting up parenthesis ()! You need one #endregion for each #region or else you will get errors.

     #region Region summary goes here
     
     //TODO: Insert code here!
     
     #endregion

When regions are in your code, there will be a box with a plus or minus in it next to the region that lets you expand and collapse it. Regions can be as long or short as you want, you can put a region around one line of code or from the first line to the last line (that would be silly though!)

Another important factor to organization is that you break your functions down into easily manageable sections, three functions that are 50 lines each are easier to read, write, and fix than one 150 line function.

Keep at it, once you start organizing things a particular way keep doing it, after a while that method of organization will just be how you write code.

The last tip I'll leave you with is that you shouldn't always be trying to reorganize things, but you should regularly stop coding new things to look over what you have so far and make sure that it is doing both what you want it to do and that you don't have useless or redundant code.

Comment
Add comment · 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

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

11 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Enemy not moving towards Player 0 Answers

Square Character Controller? 4 Answers

How do I force a GameObject with children to collapse and expand in the hierarchy view? 1 Answer

Best way to go about believable 2 Character Grab/Lift animation? 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