- Home /
Why is my character animation not working ?
Hi guys, quick question. I have no idea why my animation script is not working entirely. The first part, the trot animation works. Also the last 'else' statement is also working. The rest of the buttons like jumping and sprinting aren't working.
This is the java script that controls the animations for a horse character. Any thoughts would be welcome.
function Update () {
if(Input.GetAxis("Horizontal") || Input.GetAxis("Vertical"))
{
animation.CrossFade("trot");
}
else if(Input.GetAxis("Horizontal") && Input.GetKeyDown(KeyCode.LeftShift))
{
animation.CrossFade("run");
}
else if(Input.GetAxis("Vertical") && Input.GetKeyDown(KeyCode.LeftShift))
{
animation.CrossFade("run");
}
else if(Input.GetKeyDown(KeyCode.Space))
{
animation.CrossFade("jump_only");
}
else if(Input.GetAxis("Horizontal") && Input.GetKeyDown(KeyCode.LeftShift) && Input.GetKeyDown(KeyCode.Space))
{
animation.CrossFade("run_and_jump");
}
else if(Input.GetAxis("Vertical") && Input.GetKeyDown(KeyCode.LeftShift) && Input.GetKeyDown(KeyCode.Space))
{
animation.CrossFade("run_and_jump");
}
else
{
animation.CrossFade("idle");
}
}
Hey, What if you replace all your 'else if()' with just 'if()'?
Answer by Lazdude17 · Jul 29, 2014 at 08:33 PM
You must get rid of your else's. The first and last work because whenever you are holding up, down or left or right you are trotting. If you are not holding one of these then you idle.
As soon as your original if statement is true it will skip all the rest because they are only asking things if the original statement is false. Hence the else.
if(Input.GetAxis("Horizontal") || Input.GetAxis("Vertical"))
{
animation.CrossFade("trot");
if(Input.GetKeyDown(KeyCode.LeftShift))
{
animation.CrossFade("run");
if(Input.GetKeyDown(KeyCode.Space))
{
animation.CrossFade("run_and_jump");
}
}
if(Input.GetKeyDown(KeyCode.Space))
{
animation.CrossFade("jump_only");
}
}
else
{
animation.CrossFade("idle");
}
Remember that an 'if' statement inside an 'if' statement is an && because it requires both to be true to execute the code inside. So by putting
if(Input.GetKeyDown(KeyCode.LeftShift))
inside of the
if(Input.GetAxis("Horizontal") || Input.GetAxis("Vertical"))
statement. You are saying
if((Horizontal or Vertical) && LeftShift) { Do this. }
Thanks for your respons! I totally get what you mean by saying an if statement within an if statement means it's &&.. thank you for your very good explenation. So here's the following problem. I got rid of the else's but now the only animation that is working is the idle animation when I don't press anything. This is the script now:
function Update () {
if(Input.GetAxis("Horizontal") || Input.GetAxis("Vertical"))
{
animation.CrossFade("trot");
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftShift))
{
animation.CrossFade("run");
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
animation.CrossFade("run_and_jump");
}
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
animation.CrossFade("jump_only");
}
else
{
animation.CrossFade("idle");
}
}
I apologise for the bad lay-out within my comment above. Didn't know it would look like this. Hope you can figure it out? Else I can post the adjusted script in a new answer.
function Update ()
{
if($$anonymous$$athf.Abs(Input.GetAxis("Horizontal")) > 0.2 || $$anonymous$$athf.Abs(Input.GetAxis("Vertical")) > 0.2) //Changed this line
{
animation.CrossFade("trot");
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftShift))
{
animation.CrossFade("run");
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
animation.CrossFade("run_and_jump");
}
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
animation.CrossFade("jump_only");
}
else
{
animation.CrossFade("idle");
}
}
I think that should do it. GetAxis returns a float not a bool so I don't think its of any use to just check it by itself like you did. Negative numbers are when the stick is pointed Left(Horizontal) or Down(Vertical). These numbers only go from 0 to 1 or -1 like I said. So checking to see that it is greater than 0.2 will insure that it was not just bumped by the player but that they did it intentionally.
Let me know if this works.
Thank you again for your response. I copied the script you provided into unity but no animation will play. Whenever I move the player I think the animation is trying to play for a fraction of a second, but then it stops immediately. The animations work perfectly when triggered seperatly. It shouldn't be this hard to get the player to display the right animation at the right time ? Hmm hope you can help me figure this one out. Thanks again !
function Update ()
{
if($$anonymous$$athf.Abs(Input.GetAxis("Horizontal")) > 0.2 || $$anonymous$$athf.Abs(Input.GetAxis("Vertical")) > 0.2)
{
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftShift))
{
animation.CrossFade("run");
}
else
{
animation.CrossFade("trot");
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
animation.CrossFade("run_and_jump");
}
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
animation.CrossFade("jump_only");
}
else
{
animation.CrossFade("idle");
}
}
You may find a way to work this but I think it's a bit clunky. You should mess around with $$anonymous$$ecanim it makes things a bit simpler. You can also make a state machine to handle the state that the player is in i.e. running, jumping, walking and so forth.
Your answer
Follow this Question
Related Questions
How to handle multiple animation variants 1 Answer
import softimage mod tool 7.5 to unity, good or not? 0 Answers
Animations: just making sure my workflow is right. 1 Answer
How to play character animation after bone deparent? 2 Answers
Model gets really big when animator controller is applied? 1 Answer