If you have never worked with Mecanim before, I will give a very brief tutorial on how to set up an Animation Controller, so you will be able to understand this tutorial, but I do strongly recommend checking out the animation tutorials by Unity.
Though, because of some changes in Unity 5.0 these tutorials should also be watched when using a 5.x version of Unity3D.
Also note that Mecanim isn't limited to use for 3D models, it can also be used on sprites. Sometimes this requires a little bit of tweaking, but Unity Technologies is working on improving this feature.
STEP 0:
Have a 3D model with animations, made in your favorite 3D modeling/animating program. There are multiple ways to get a model ready to do fancy things in Unity, but I prefer having all the animations in one file with the model and exporting it as a .FBX file. That way all animations are aligned as a sequence which can later be cut apart in Unity, or pre-cut in the animation software.
STEP 1:
Add the Animator component to your model's root (if it doesn't already have one). A lot of things regarding setting a model up for animating in Unity are done automatically when importing a model.
STEP 2:
Cut the animation sequence into clips and name them accordingly to make it easier to align them later (if this wasn't already done in the animation program). In my experience, animations work best when they have a start-up clip and a looping clip. Sometimes a wind-down clip will look nice as well, but you will have to take the extra time taken into consideration. (tl;dr Don't do this if your character stops dead in his tracks when moving)
STEP 3:
Create the state-machine for your animation! This state-machine is known as the Animator Controller. Create a new Animator Controller, open it. Now drag all the animation clips you made last step into the Animator view.
STEP 4:
Alright, this is the "difficult" step. You now have to connect the different clips. The connections between the clips determine from which clip another can be called. So, you'll probably want the "Idle" clip to connect to every other one, but the "Run" only to the "Walk".
Here's an example of how a finished Animator Controller state-machine would look like:
STEP 5:
Now, as you have been connecting the different clips, you might be wondering: "How will the game know when to activate certain clips?" That's because now, we're going to be adding different parameters that serve as rules for activating the connections. There are a few different types of parameters and they all have to be accessed from code.
- Float
- Int
- Bool
- Trigger
STEP 6:
Now, you just have to access the parameters through your scripts. This can be done very easily.
private Animator anim;
void Start ()
{
anim = PlayerModel.GetComponent();
}
void Animate ()
{
anim.SetTrigger("Firing");
anim.SetBool("Blocking", true);
anim.SetFloat("Walking", speed);
anim.SetInt("Running" speed);
}//
Go ahead and try out some new things! Good luck!

Geen opmerkingen:
Een reactie posten