How to Repurpose Unity’s Third Person Character Controller Animations for Your Own Game
In the previous article, I outlined how to connect a static 3D character to Unity’s Third Person Character Controller Starter Assets Pack¹. This takes advantage of the Starter Asset’s built in animations and keyboard controls and integrates those capabilities into any other static 3D character model of your choosing.
But depending on the project you’re working on, you may not want the player to control the character with keyboard controls. I’ve recently had the great fortune to work as Lead Unity Developer for a Game Development startup that’s developing a hyper-casual game that is somewhat modeled after Zynga’s Farmville 3.
In Farmville 3, the characters in the game are controlled by Artificial Intelligence (AI) and other scripted and/or event based behaviors. The characters in Farmville 3 move around on their own and will deliver dialogue and other interactions when certain events are triggered. That means that the character’s animations need to be triggered based on scripted behaviors, not keyboard controls. For example, if I want one of my characters to walk from point A to point B, then I need to be able to trigger a walking animation for that character once the movement has been triggered.
We can do something similar by using Starter Asset’s built-in animations, without the need for any of the built-in keyboard controls.
To make this work, we need to do the following:
- Ensure you have Unity’s Third Person Character Controller Starter Assets installed and imported into your project. Also, I’ll be using Synty Studio’s Low Poly Farm Asset Pack² characters. Any Synty Studio asset pack with characters will work fine.
- We will identify the Starter Asset folder locations for the desired animations we want to use.
- We will create a new Animation Controller using the existing Starter Assets animations for idle and walk.
- We will connect the animation controller transitions between the desired animations and also create a new integer parameter that can be accessed and set inside of our character AI script.
- We will implement the animations inside the character’s AI script by creating a reference, or instance, of the animation controller. Finally we will test the implementation at run-time.
Setup
To setup, go ahead and drop any of the Synty Studio characters onto your scene. My Scarecrow character is in a static T-pose when initially dropped into the scene and will remain that way when running the game since it doesn’t have any animation or controls built-in. Figures 2 and 3 below illustrate a simple movement AI attached to the Scarecrow and the resulting translation of the character across the scene.
With the scene and static character setup, let’s dive into creating the new Animation Controller using the Starter Asset’s built-in animations.
Create a New Animation Controller
Under your Assets folder, create a new folder and call it Animations. Under the new Animations folder, right click inside the empty folder and on the dropdown, select Create/Animation Controller. We will call this new Animation Controller “ScarecrowAnimations.” We need the Animator Window so, go to Window/Animation/Animator and this will bring up the Animator Window. Go ahead and dock this next to your Scene Tab. When you click on the Animator Tab, you will see the default states for Entry, Any State and Exit.
Now, what we want to do is to bring in the Starter Pack’s animations for idle and walking so we can connect these animation states to these new default states.
Identify the Location of Starter Pack’s Built-In Animations
For this step, make sure you’ve first imported Unity’s Third Person Character Controller Starter Assets. Next, go to the following directory inside your Unity Project Assets folder: Assets/StarterAssets/ ThirdPersonController/CharacterAnimations.
Once there, look for the Idle and Walking animation icons. You can preview those animations in the Inspector once the desired animation is selected. Figure 5 shows the preview of the walking animation.
Next, drag both the Idle and Walking Animation icons (highlighted in Figure 5) into the Animator Tab. Note, this is the Animation Controller we created in the previous step.
Connect Animation Controller Transitions
We now have all the desired animation states inside of our Animation Controller. Now, let’s connect the transitions. When the game starts, the default state is the “Entry” state. From Entry, we want to flow right into the Idel Animation. We can make the Idle Animation the default animation by right clicking the Idle Block and select, “Make Default.” We also want to be able to go from “Any State” to “Idle” and from “Any State” to “Walking.” Right click on “Any State” and select “Make Transition” and then connect the transition to the “Idle Block.” Repate this step to connect from “Any State” to “Walking.” When it’s all finished, your Animation Controller should look like the one in Figure 7.
Next, let’s create a new Integer Parameter. To do this go to the Parameters Tab and click the “+” icon and select “Int” from the dropdown.
In the new Window, call the Integer Parameter, “AnimState.”
Configure the Animation Transition Settings
Click on the transition arrow between “Any State” and “Idle.” In the Inspector, set the following Transition Settings:
- Fixed Duration: off
- Transition Duration: 0
- Can Transition to Self: off
- AnimState Parameter Condition Equals 0 (for idle)
- Repeat this step for the Walking Transition with AnimState Parameter Condition set to 1 (Walking). These integers will get used inside the Scarecrow AI I showed earlier in Figure 2.
Connect Character’s Animation Controller
Now, we just need to reference the new Animation Controller we just connected, inside the Scarecrow AI script. Figure 12 shows the 2 extra pieces of code that I included in the script.
I commented out the portions of the script that translate the Scarecros across the scene so I can have the character stand still while I test to see if it will animate the default Idle state. Figure 12 shows that the Scarecrow defaults to the Idle animation after starting the game. The state machines in Figure 8 show that after the game starts, the animation will transition right into Idle by default, without having to specify the Integer “AnimState” parameter.
Connect Walking Animation to AI Script
Next, we simply want to set the “AnimState” Integer parameter to 1 whenever the Scarecrow begins to walk. Earlier, we set the Walking Transition “AnimState” integer parameter to 1. So, whenever we set this integer parameter to 1 inside our script, the Animation Controller will transition to the walking animation. To do this, we write the following command inside the code segment where the character is being translated across the scene:
- animator.SetInteger(“AnimState”, 1);
We also want to make sure that once the character stops moving, the “AnimState” is set back to 0 (for idle).
Figure 13 below shows the additional C# script I used to set the AnimState integer parameters.
Testing the Animation Transitions
Now, we can test our animation. The demo below shows the final results.
We can now control the character movements through scriptable behaviors. We’re a lot closer now to being able to building scripted character behaviors like the ones we see in games like Farmville 3.
Thank you for checking out this article.
References
[1] Unity’s Third Person Character Controller Starter Assets
https://assetstore.unity.com/packages/essentials/starter-assets-third-person-character-controller-196526
[2] Synty Studio Low Poly Farm Pack
https://assetstore.unity.com/packages/3d/environments/industrial/polygon-farm-low-poly-3d-art-by-synty-146192
Check Out My Portfolio Here:
https://ricardoemiranda.com/
(under construction)
LinkedIn Profile:
https://www.linkedin.com/in/ricardoemiranda/
Check Out My Other Articles Here:
https://medium.com/@ricardoemiranda