• 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 Teah · Sep 26, 2014 at 10:14 PM · animationjavascripttexturejumping

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

okay so, im making a simple 2D platform game and i have this problem.

Basically i used the Animating tiled texture script from Unity to add a run, idle and jump animation to my player, heres what it looks like:


 #pragma strict
 
 
 function Start () {
 
   }
    
    
 function Update () {
     var at = gameObject.GetComponent(animatetexture); //Store AnimateTexture Script
     if(Input.GetAxis("Horizontal")){ //Player moves left
         at.rowNumber = 1; //Change to running animation
     } else if(Input.GetAxis("Horizontal")){ //Player moves right
         at.rowNumber = 1; //Change to running animation
     } else if(Input.GetButton("Jump")){
         at.rowNumber = 2; //Change to jump animation
     } else { //Player doesnt move
         at.rowNumber = 0; //Change to idle 
         }
         
 }          

        


but the problem is, when i jump and move left or right while still in the air, my jump animation stops and goes to the running animation, but if i were to press the space bar alone to jump, it works fine.

i also want to make my jump animation continue on until my player touched the ground ( so my fall animation as well)

please, i hope someone can help me figure out how to fix that, because it really bothers me lol

thank you!

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 robertbu · Sep 26, 2014 at 10:15 PM 0
Share

For future posts, please format your code. After pasting, select your code and use the 101/010 button. I did it for you this time.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Baste · Sep 27, 2014 at 08:50 AM

Your code makes no sense!

This stuff:

 if(Input.GetAxis("Horizontal")){ //Player moves left
     at.rowNumber = 1; //Change to running animation
 } else if(Input.GetAxis("Horizontal")){ //Player moves right
     at.rowNumber = 1; //Change to running animation

Checks the exact same thing twice, and does the exact same assignment twice. The second check will actually never be run, and does nothing. What you want to check is if Input.GetAxis("Horizontal") is returning a value that's larger or smaller than 0, to get whether your player is moving to the right or left.

To fix your actual problem, you need to check if your player is in the air or not, and only play the run animation if the player is grounded. There's two main ways of doing this:

  1. Use a raycast to check how far away from the ground your player is. If it is further than a certain amount, you're in the air.

  2. Attach a ball-shaped trigger collider to your player's feet. Give it a script that notifies the player of when it is touching the ground or not.

I would generally recommend the second one, as it's easier to modify where the collider is than to modify the distance of the raycast, as you can see the collider in the inspector.

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 Teah · Sep 27, 2014 at 09:10 PM 0
Share

Oh i see, thank you, ill give it a shot then

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Fall animation when character is falling 3 Answers

Need help with sprite animating 1 Answer

Scripting and Animation problems 1 Answer

Most efficient way to do 2D animation 2 Answers

Animation play when dead 2 Answers

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