Could someone give a newbie some tips on character animation

Hey people. I’m quite new to Unity… .I’m slowly working my way through the game and scripting tutorials and I have a question regarding moving my character.

I have made a model in C4D which has a ‘walk forward’, ‘walk backwards’ ‘sidestep left/right’ and an ‘idle’ animation (all in one file at different points in the timeline).

I have exported the file into Unity, and separated out all the different animations in the ‘Settings’ window in the Project section. And I basically want to make the animations play (and loop) when a certain key is pushed…

for example…
W= play the ‘walk forward’
S = Play the ‘Walk Backward’
Nothing pushed = ‘Idle’
etc etc etc…

So far I have been mildly successful… I have managed to differentiate between ‘Idle’ and ‘walk forward’ via a piece of code using CrossFade… This is the code I have used…

So thats fine… he stays still and plays his ‘idle’ animation when nothing is being pushed… then when I push the W key he walks forward and plays his ‘walk forward’ animation (I’ve already set a separate code to make him move to the front, back, left, right and rotate.), also there is a smooth transition between the 2 animations…

However this is where I am stuck… I have been trying and trying… and I just cannot find a successful way to include the ‘walk back’ and ‘sidestep left/right’ animations into this code…
Basically I do not have enough knowledge of coding to know how to add more animations into the script… So my question is… Given the script I have shown you… how could I also include OTHER animations to it?
I want him to also walk backwards, sidestep, and maybe even things like jumping and crouching… but I just need to know how to add more animations to this script…

Thanks for taking the time to read and reply :stuck_out_tongue: .

Well, the question is whether you’d like to have running forward or running sideways have a priority. Depending on the answer, you might want to use Input.GetButton instead of GetAxis.

For instance, if you only want the current button-press to be read, you should do something like the following:

if (Input.GetButton(“Left”)) {
if (Input.GetButton(“Forward”)) {
Animation.CrossFade(“WalkForward”);
}
if (Input.GetButton(“Backward”)) {
Animation.CrossFade(“WalkBackward”);
}
else {
Animation.CrossFade(“SidestepLeft”);
}
}
if (Input.GetButton(“Right”)) {
if (Input.GetButton(“Forward”)) {
Animation.CrossFade(“WalkForward”);
}
if (Input.GetButton(“Backward”)) {
Animation.CrossFade(“WalkBackward”);
}
else {
Animation.CrossFade(“SidestepRight”);
}
}
if (Input.GetButton(“Forward”)) {
Animation.CrossFade(“WalkForward”);
}
if (Input.GetButton(“Backward”)) {
Animation.CrossFade(“WalkBackward”);
}

Of course, this requires that you put in the descriptive word “Left” for Horizontal negative, “Right” for Horizontal positive, “Forward” for vertical positive, and “Backward” for vertical negative. Also, I’m assuming that the animation name I put in is the same as yours, although it probably isn’t.

Other than that, the logic is as follows: Forward and backward have priority, so if they are pressed they override left and right’s animations. This should work so long as the players don’t go crazy with hitting every button at once. (That’ll cause the run forward animation to play in a blend with run backward. Very interesting.)

As an additional measure, you might want to set all the animations to the same layer.

Edit: Whoops! Forgot idle. Hmm… Well, you could have one if statement check if any button is being pressed using the or || command and have the else be Animation.CrossFade(“Idle”);

The solutions for things like Jumping and Crouching are a tad more difficult. You’d need a set of animations for all the movements during the crouch, for example…

If Jump and Crouch are stationary movements, you can just crossfade to them like any other animation… Within limits.

For Jump, you’d need to check if you’re in contact with the ground and have it CrossFade if you’re in the air. (The jump key-press would move the player upwards if the player is on the ground)

For Crouch, you’d just do the same as my example, only with the crouching animations. Just have the condition if (Input.GetButton(“Crouch”)) below the animations and have the else be my above example.

Lots of lines, but in the end it looks quite good and goes back to normal animations after the key isn’t pressed.

Hey thanks a lot for the response… It has helped a lot…

I typed in the code you gave me, I just need to change ‘Animation’ to ‘animation’ and it worked great.
However the only problem I am now having is the idle issue…

I have tried a number of things, but I am beat as to how I can get it so when NO buttons are being pushed it resets back to ‘idle’…

I guess I need a

else {
animation.CrossFade(“idle”);
}

which somehow applies to everything… but I cannot for the life of me work out how I would go about it… I have tried a number of different ways, but I just am not good enough with Java script so far… Any help on how to do this would be great right now.

Well, the solution for that would be a monster “If” statement. Basically, it’d go like:

if (Input.GetButton(“Forward”) || Input.GetButton(“Backward”) || Input.GetButton(“Jump”) || Input.GetButton(“Left”) || Input.GetButton(“Crouch”) || Input.GetButton(“Right”)) {
// animation code for movements
}
else {
animation.CrossFade(“idle”);
}

Which means: If any movement button is pressed, go to the specific animations. Otherwise, idle.

Hi,

I’ve tried this but Unity gives an error that || doesn’t work.

And how do I implement StrafeRight, StrafeLeft, Forward, Backward? (I do have the code but how do I setup the function?)