1
\$\begingroup\$

My team is currently building a 2D game in Unity and we've run into some workflow/script organization blockades for many of our game's characters.

For example, we're not sure what the standard method of encapsulating/organizing a character's movement, animation, and more complex AI code is so that things don't become a behemoth mess.

Originally, we had all a character's code in one file -- movement, animation, and AI decision-making code. Needless to say, this became messy really quickly and horrendous to debug.

Then, we redesigned it so that there were three scripts on the character: a movementHandler, animationHandler, and AIHandler. These scripts would communicate to each other, but encapsulate their own functions. This worked better than before, but also grew to a point where it was a bit messy organizationally and debugging is becoming a nightmare again.

We're interested in knowing how people typically organize their scripts. Is there a way people know to organize these code sections so that they're more scalable and don't become headaches to manage once the project grows and grows?

\$\endgroup\$
1
\$\begingroup\$

In my opinion the best is start with a good AI layer that uses a state machine approach (here and here). Making it simple, the current state AI evaluate some inputs, make a decision and eventualy jumps to a new state. The movement and animation layer relay on actual AI state to do their stuff

\$\endgroup\$
1
\$\begingroup\$

I'm not experienced programmer, but as for me, I try to divide as much logical parts as I can into different scripts. For ex, for the character you can use something like MVC, model-view-controller pattern. The point is that one script processes user input and communicates with tho other parts which are responsible for animations and actual movement.

In Unity scripting most of the usual programming patterns are true, like using fabric to generate different kinds of creatures with similar logic, or singletons, etc.

Though there are some game-specific things which you can find on forums and in books like Robert Nystrom's 'Game programming patterns'

\$\endgroup\$
  • \$\begingroup\$ So according to this, would you recommend separating movement and animation into different scripts or the same one? \$\endgroup\$ – Jestus Aug 21 '15 at 5:12
  • \$\begingroup\$ Separated things are always easier to debug and are more flexible. The answer from dnk describes a good approach, I've used something similar, but less complex. My Animator script had a set of states, which were set from the Controller script based on input or current MoveVector and velocity. Animator was applying animations based on curent state. And also I had Motor script which was apllying actual movement, gravity if not grounded etc. \$\endgroup\$ – Phoera Aug 21 '15 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.