Making a 3d Unity Game - Ep 1 Setup

  5 mins read  

In today’s post I will import the animated model created in Episode 5 into Unity and being crafting a demo scene.

Note: I will assume some basic understanding of the tool in these tutorials - if something is unclear let me know in the comments and I will address it

Creating a scene

Before I imported the model I wanted to make sure I could even implement the concept I had thought of which is having an ant be able to walk over all surfaces in a world. Considering this was the sole premise upon which all of the ideas for the game rested upon I wanted to first make sure I could even do that. Beginning with an empty scene I created a plane with a couple large shapes which were all static. I then created a placeholder capsule that would act as my ant and allow me to explore the movement mechanics. Once the capsule was in place I did a quick search to see if anyone has solved this problem in some capacity and came across a great thread on the Unity forms. With this controller in place and modifying it for my needs I already had the first milestone complete - being able to walk on walls.


The premise of this controller is that there is a constant (gravity) force applied in the opposite direction of the player normal. This means no matter what surface the player is on gravity will be pushing it towards the surface underneath. The other major component is the logic for determining when to transition to a wall - for this a Ray is used. The ray is cast from the player’s center in the forward direction Note: not world forward, but local forward. If the ray hits something it means the player is running into it so a transition is started that lerps the player’s position and rotation to the wall over time using a Coroutine.

Importing the model

Now that the core controller was in place I was ready to import the model into Unity and hook up the animations. From Blender I exported the model as an FBX file making sure to use Selected Objects option and only import the mesh and armature. The FBX file was placed in my Unity project’s Models folder. In Unity the model now appeared and I simply swapped out the capsule with the imported model.

Animations

With that swap I now had my model in place and moving, of course it was pretty terrible at the moment without animations. I double checked the animations had imported correctly by clicking on the ant model in my Models folder, going to the Inspector, clicking on the Animation tab and locating all my animations under the Clips section. Everything was there which was good.

The next step was to setup the animations. Up until this point I’ve only used Unity animations for simple things like moving objects which I was pretty sure was not what I wanted to do with this. After searching the web for animating a bone-rigged model I came across the MechAnim functionality, and specifically the Animator.

Animator

The Animator is basically a giant state machine. There are a few special states created for you like Entry, Exit, Any State, and the default state. My first goal was to get the idle animation playing so I used the default state as Idle and mapped the state to the Idle animation from my model. After adjusting the scale I had it playing exactly how I wanted for the time being. The next step was to setup walking. To do that I created a new State called Walking and added a transitioned from the Idle state. I quickly realized I had no way to filter this transition so for that I added a speed parameter. Now I needed to provide that parameter to the Animator.

Events

From my last Unity project I wanted to focus on good code this time around so decided that I’d use events in this case and create an AnimationDataProvider component that set all necessary data for my Animator. After another search I came across a great tutorial on Unity that outlined a simple messaging system. This was exactly what I was looking for! After implementing this class and hooking up an event for when the player is moving in the controller I now had a functioning speed parameter. Once hooked up I went back to the Animator and updated the transition from Idle to Walking to add a condition where speed > 1. Similarly I added a transition back to Idle from Walking if speed < 1. I now had my walking animation in place!


Things were starting to look halfway decent!

Takeaways

  1. Exporting from Blender is pretty easy if you use FBX and remember to only export Mesh and Armature.
  2. You’ll want to use the Selected Objects` option on export if you have other stuff in your scene.
  3. The Animator is a powerful tool for crafting animations - learn it!
  4. Searching for answers before embarking on your own can sometimes fast-track you.

Stay tuned next time where I add some more animations to my model and use Unity’s post-processing library to create some nice effects.