Recently I wrote an article about a theoretical approach towards creating a combo system for a fighting game I’d thought on (notes here). This included the use of a queue to keep track of input in the branching combo tree. I wanted to write up a mini post-mortem into how I got on within Unity on this stage of the project.

The first step was establishing a character and object to attack with. I’m a great fan of Norse mythology and having watching the first season of Vikings, I had a loose setting for how I’d want my game to look and feel, it’s important to note however that I’m focusing primarily on just coding and design, the visual design will remain largely untouched. Thankfully Unity accommodates this with the 3D objects you can create such as a cube.

With my player (a long cube) and my hammer (a normal cube) set, I needed a way to inform how they should interact. Typically everything would require a collider for later use when interacting with enemies and trigger states, but the main issue was having the player be able to move and swing their hammer and how to have that interaction play out. Unitys Mecanim is incredibly versatile and the right tool for the job I figured, I could have the player move around and swing into action, literally, based on a variety of inputs and states. The hammer would move in an animation and this would suffice for a swing in my proof of concept.

Unity has given the objects and the Mecanim class to allow for movement, now it was a case of recording key frame to establish movement, again these were kept nice and simple. This means that even the clips were made now for my varying attacks, these attacks were honestly created by me just swinging an imaginary hammer and thinking how I’d naturally try and move.

AnimationTreeForum

This is where the bits and bats started to crawl in, when dealing with the actual code. The basics were quite straight forward, I could establish an Enum that would count as states to be in that could be passed to the Animator as int values for parameters and that would dictate the flow of the animations. Then it’s a case of checking input of 2 options, swing or throw and adding this to a queue. This means when items were peeked at in the Queue they would then set the Enum state which would pass to the animator and set which animation the user should be in, and then finally dequeue the attack value.

The biggest issue was getting the animator to go back to Idle when needed but to allow time for combo’s to be filled out in full. Leaving the Idle transition as blank didn’t allow for a combo to be built up, thus making the queue pointless unless I wanted to have a star type approach to the animation where any state could technically be transitioned to at any time. This is entirely possible with the solution that I have now, however I felt it should flow correctly and not use Idle as a hub between animations.

Time was the cause of the issue. All the animations have an exit time, however if the animator decides half way through to clear out the Queue and set the state to idle, or worse clear it every frame call and set to idle, then naturally you can’t allow for both a combo that is either complete or incomplete. The solution was quite straight forward, the Mecanim clips actually show how long the clip lasts over, all mine happen to be 1s long or 60 samples, this means that if a value of “comboTime” is set to be 1s and is removed from every frame for the amount of time it took between frames, then we have a window of opportunity for the Queue to be built.

Although a simple solution, it still took some tweaking of the animator to get to the point of getting it right, and all in close to 100 lines (Although if you scrap comments who knows!). The script as it stands at the time of writing can be found attached to this article at the bottom for your own viewing, here is a gif of the animation as it currently stands:

MightyMjolnirGif
This should capture all combos available as it stands. Obviously using a gif lowers quality of movement so it’s not as slow as this when actually in Unity.

Here is the link to the code sample as mentioned above.

Thank you for reading this far, if you’d like to reach out please do so on Twitter or just hang around this site, there’s sure to be new articles coming along soon, specifically regarding Journey & Destiny at time of writing. Have an awesome day and keep on designing.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s