In this chapter we will explain how to replicate animations over the networking using Bolt. There are many ways to do this, even many ways inside Bolt, we are going to look at the default way of replicating mecanim parameter state over the network. This chapter will be rather short, as there is not that much to write about mecanim replication, but it's such an important topic that it deserves its own chapter.
Creating A Mecanim Asset
The first thing we are going to do is to hook up the Animator Controller for the character we are using, you can find the controller in bolt_samples/AdvancedTutorial/art/models/sgtBolt/sgtBolt4merged. Follow the next steps:
- Select the sgtBolt4Merged-ModelOnly sub-object on your TutorialPlayer prefab;
- Open the folder bolt_samples/AdvancedTutorial/art/models/sgtBolt;
- Find the Animator Controller sgtBolt4merged;
- Drag it into the Controller slot on the Animator component;
- Disable Apply Root Motion;
- Set Update Mode to Animate Physics.
When the animator controller is connected, double-click on it to open the unity animator editor.
To explore the available parameters, click on the Parameters tab. Here you can see several parameters, but for now we are only going to use MoveZ, MoveX and Jump. MoveZ and MoveX are floats and Jump is a trigger. Go to the Window/Bolt/Assets window and open up the TutorialPlayerState we created earlier.
We are going to define three new properties here, called the same thing as the three ones from the mecanim animator we are using.
- MoveX, Float
- MoveZ, Float
- Jump, Trigger
In the settings for each property, switch the mecanim setting from Disabled to Parameter.
There is one more option we need configure, to the right of the Disabled/Parameter/Layer Weight selector for mecanim you also have the option of selecting between Use Animator Methods and Use Bolt Properties.
- Use Animator Methods means that you want to use the normal
SetInteger, etc. methods of mecanim and have Bolt automatically pull the value out.
- Use Bolt Parameters means that you want to use the Bolt parameters to set the mecanim values and have Bolt apply them to mecanim for you.
You can also configure this state wide at the top in the state specific settings. We are going to select Use Bolt Properties on MoveZ, MoveX and Jump.
We are almost done, it's time to compile Bolt again, go to Assets/Bolt/Compile Assembly and let Bolt do it's thing. Now open up the TutorialPlayerController.cs script.
We are going to add a new method called AnimatePlayer, its pretty much standard mecanim code where we set a MoveZ parameter to -1 or +1 depending on if we are moving forward/backward and the same with MoveX for left/right.
Inside of the
ExecuteCommand function we are going to call our new
AnimatePlayer function, we are going to access a very specific property on our command called
If this is true that means that this is the first time this command is being executed, in that case we want to apply our animations.
The last thing we need to do is to pass in the correct animator to Bolt, we will do this inside the Attached callback where we are setting our transform:
You can start the game and run around with our character now, you can choose to just Play As Server or also connect clients if you want.