- Home /
Creating my own character animation controller
So I'm trying to learn more with unity, and I would like to be able to animate my character. On one PC, I downloaded the Penelope tutorial to see how they did animations - and it looks like a mess:
The Update function is going to contain a lot of conditional checks to determine which animation
to play. Before we start writing them, we need to get some information about Penelope’s
horizontal and vertical movement.
Uh, this seems like a disaster waiting to happen - if my character controller already contains this logic (e.g., jumping, etc.) why should I have to duplicate it?
My thought is this. I have my Character Controller, and I create a public variable named "characterState". I could set this variable to "IDLE" or "RUNNING" or whatever, it just needs to store what the character is currently doing. I could then write a seperate Animation Controller that stores a local private copy of this status, and constantly looks to see what the CharacterControllers "characterState" variable is doing. If the local vs. public variable is different, update it locally and play a new animation.
Here's some pseudocode to explain my idea
CharacterController
{
public myCharacterState as ENUM_CHAR_STATE { Idle, Run, Walk }
Update()
{
myCharacterState = IDLE
if (INPUT = FORWARD)
myCharacterState = WALK
}
}
AnimationController
{
private localCharacterState as ENUM_CHAR_STATE { Idle, Run, Walk }
Update()
{
if (localCharacterState != CharacterController.myCharacterState)
{
if (CharacterController.myCharacterState == IDLE)
playAnimation(IDLE)
if (CharacterController.myCharacterState == WALK)
playAnimation(WALK)
if (CharacterController.myCharacterState == RUN)
{
if (localCharacterState == WALK)
{
// blend animation
} else {
// replace animation
}
}
//etc....
localCharacterState = CharacterController.myCharacterState
}
}
}
This is just some rough pseudo-code I just threw together to get my point across. However, since Unity is all new to me, what are the disadvantages that I could experience by programming it in this fashion?
EDIT: I just thought of something, what if the player is RUNNING and JUMPING? Or FALLING and HIT WITH A ROCKET? I would argue that the most recently applied animation should be the one that plays on through - the animation shouldn't determine gameplay. And in the case of FALLING and HIT WITH A ROCKET, if getting injured while falling cancels gravity and let's you float in the air, that's just poor programming to begin with ;)
You should check out the 3D Buzz tutorial, it covers having an animator class : http://www.3dbuzz.com/vbforum/content.php?212
scroll down to : Section 3 - Enhanced Character and Camera Control : 7 - TP_Animator Overview and Theory
They use 4 classes : Camera, Controller, $$anonymous$$otor, Animator
http://www.alucardj.net16.net/unityanswers/TPC_Tutorial/TP_Camera.cs
http://www.alucardj.net16.net/unityanswers/TPC_Tutorial/TP_Controller.cs
http://www.alucardj.net16.net/unityanswers/TPC_Tutorial/TP_$$anonymous$$otor.cs
http://www.alucardj.net16.net/unityanswers/TPC_Tutorial/TP_Animator.cs
So - basically, it's exactly what I stated except that they split the input and character controls into their own separate classes - which is a smart thing to do (I'm currently working off of the base scripts so just starting off). EDIT: Noticed that they moved the actual movement code into the animator class (which I dislike)
yep, I upped the scripts hoping they would be a useful reference. Agreed all the movement should be in the $$anonymous$$otor script. Hopefully an experienced programmer can give more info on what are the disadvantages that I could experience by program$$anonymous$$g it in this fashion.
Ok, well this is a good reference. I'm glad you agree too - I'm a fan of decoupling (though my code sometimes doesn't reflect it!) Ideally, the code should be a one-way weak relationship. If I have my input class, it should still work even if I remove the $$anonymous$$otor class. Same for Animator class, if I remove it the $$anonymous$$otor class should still work. Now, if I remove the input class, I would be dumb to assume that any class that relies on it to continue working.
Your answer
Follow this Question
Related Questions
Third Person Controller 1 Answer
Climbing stairs and climbing on a slide 0 Answers
Player Animation and control panel 2 Answers
Character controller movement 1 Answer
independent arm from body - unity2d 0 Answers