Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by NerdGameStudio · Jul 29, 2014 at 08:00 PM · animationcharacternot working

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");
     }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Jul 29, 2014 at 08:23 PM 0
Share

Hey, What if you replace all your 'else if()' with just 'if()'?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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. }

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NerdGameStudio · Jul 29, 2014 at 08:56 PM 0
Share

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");
    }

}

avatar image NerdGameStudio · Jul 29, 2014 at 08:58 PM 0
Share

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.

avatar image Lazdude17 · Jul 30, 2014 at 01:45 AM 0
Share
 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.

avatar image NerdGameStudio · Jul 31, 2014 at 11:09 PM 0
Share

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 !

avatar image Lazdude17 · Aug 02, 2014 at 05:45 AM 0
Share
 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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges