• 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 HolBol · Oct 21, 2010 at 10:05 PM · animationhorizontalvertical

Get walk animation to play on horizontal and vertical axis presses.

I have this script:

function Update () { if (Input.GetAxis("Vertical")) {

animation.CrossFade("Walk"); } else { animation.CrossFade("Idle"); } if (Input.GetAxis("Horizontal")) { animation.CrossFade("Walk"); } else { animation.CrossFade("Idle"); } }

what i want to happen is that if the player presses either the horizontal or vertical axis buttons, to play the walk cycle, and if not, play the idle. However when i use this script it will play the walk on the horizontal key presses but not on the vertical ones. How do I solve this?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by VS48 · Oct 21, 2010 at 10:19 PM

Your vertical input is completely ignored because your "if-else" block for horizontal input will always override what the vertical input did.

What you probably want is:

function Update () 
{
    if (Input.GetAxis("Vertical")) 
    {
        animation.CrossFade("Walk");
    }
    else if(Input.GetAxis("Horizontal")) 
    {
        animation.CrossFade("Walk");
    } 
    else 
    {
        animation.CrossFade("Idle");
    }
}

Or something similar

Comment
Add comment · Show 1 · 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 HolBol · Oct 21, 2010 at 10:26 PM 0
Share

cheers dude :)!

avatar image
0

Answer by Setzer · Mar 30, 2011 at 10:13 PM

function Update () 
{
    if (Input.GetAxis("Vertical") || Input.GetAxis("Horizontal")) 
    {
        animation.CrossFade("Walk");
    }    
    else 
    {
        animation.CrossFade("Idle");
    }
}

Same as above, honestly, except you only have one condition and one default. If you're unfamiliar with script, "||" means "OR".

Comment
Add comment · Show 1 · 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 HolBol · Apr 02, 2011 at 11:07 AM 1
Share

umm, this question was marked as answered 5 months ago.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How can I play different animations depending on the direction my character is moving? 1 Answer

Play animation for horizontal and vertical movement? 1 Answer

limit AddRelativeForce 1 Answer

Can I make animations snap to a frame? 2 Answers

How to detect Double Clicking Horizontal/Vertical Axis on joystick 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges