• 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 eivhys · Dec 17, 2013 at 09:03 AM · animationjavascriptjumpingspacebar

Need help with sprite animating

OK, so I'm new to unity and me and a friend want to make a 2D game. So I've followed a tutorial on youtube and write this script:

Now, I've done some simple coding and programming before (but never in javascript) and i can understand the code.

With this script the animation changes between walking / jumping / idle (it is connected to this javascript http://wiki.unity3d.com/index.php/Animating_Tiled_texture_-_Extended), but when space (the jump button) is pushed and the sprite jumps, if you keep holding space the character will still be in the jump animation. How c

So here comes the question:

What can I do to make the character change back to the idle animation when hitting the ground (when the player is holding the jump button)?

And can I have another animation for when the character is on his way down?

Note: English is not my first language so I'm sorry that my formulation isn't the best

 #pragma strict
 
 
 
 
 function Start () {
 
 }
 
 function Update () {
     
     var AT = gameObject.GetComponent(AnimateTexture); //Store the AnimateTexture script
 
     if(Input.GetKey("space")) { //Player jumps
         AT.rowNumber = 2; //Start jumping animation
     }
 
     else if(Input.GetKey("a")) { //Player moves left
         AT.rowNumber = 1; //Start running animation
     }
         
     else if(Input.GetKey("d")) { //Player moves right
         AT.rowNumber = 1; //Start running animation
     }
     
     else if(Input.GetKey("left")) { //Player moves left
         AT.rowNumber = 1; //Start running animation
     }
     
     else if(Input.GetKey("right")) { //Player moves right
         AT.rowNumber = 1; //Start running animation
     }
     
 
     
     else { //Player doesn't press anything
         AT.rowNumber = 0; //Return back to the idle animation
     }
     
     
 } 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by KellyThomas · Dec 17, 2013 at 10:13 AM

At the moment animation is dependent entirely on input..

Normally animation would depend on character state (walking, jumping, punching etc).

Then you would allow the environment and player input to control character state.

For example:

  • if the character is grounded (known to be on the ground) and the player presses jump then let the character jump (apply vertical movement and play the jump animation).

  • if the character is grounded and the player enters no input play the idle animation.

With these two rules the player will transition idle to and back to idle appropriately..

As you can see the correct animation depends the character state, which is effected by both input and environment.

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

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

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Looking for script to activate door animations 1 Answer

Textured jump animation moving left and right while in the air -help! 1 Answer

I'm confused with the animator 1 Answer

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