Search Unity

Animation in air.

Discussion in '2D' started by JaimieVos, Apr 2, 2016.

  1. JaimieVos

    JaimieVos

    Joined:
    Jun 21, 2015
    Posts:
    193
    Everything works fine except attacking. When i press the attack button he do this:


    He jumps into air and then he goes back.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. publicclassPlayer:MonoBehaviour{
    6.  
    7. privateRigidbody2Drb2d;
    8.  
    9. privateAnimatormyAnimator;
    10.  
    11. [SerializeField]
    12. privatefloatmovementSpeed;
    13.  
    14. privateboolattack;
    15.  
    16. privateboolfacingRight;
    17.  
    18. publicGameObjectrigidbody;
    19.  
    20.  
    21. voidStart()
    22. {
    23. facingRight=true;
    24. rb2d=rigidbody.GetComponent<Rigidbody2D>();
    25. myAnimator=GetComponent<Animator>();
    26. }
    27.  
    28. voidUpdate()
    29. {
    30. HandleInput();
    31. }
    32.  
    33. voidFixedUpdate()
    34. {
    35. floathorizontal=Input.GetAxis("Horizontal");
    36.  
    37. HandleMovement(horizontal);
    38.  
    39. Flip(horizontal);
    40.  
    41. HandleAttacks();
    42.  
    43. ResetValues();
    44. }
    45.  
    46. privatevoidHandleMovement(floathorizontal)
    47. {
    48. if(!this.myAnimator.GetCurrentAnimatorStateInfo(0).IsTag("Attack"))
    49. {
    50. rb2d.velocity=newVector2(horizontal*movementSpeed,rb2d.velocity.y);
    51. }
    52.  
    53. rb2d.velocity=newVector2(horizontal*movementSpeed,rb2d.velocity.y);
    54.  
    55. myAnimator.SetFloat("speed",Mathf.Abs(horizontal));
    56. }
    57.  
    58. privatevoidHandleAttacks()
    59. {
    60. if(attack&& !this.myAnimator.GetCurrentAnimatorStateInfo(0).IsTag("Attack"))
    61. {
    62. myAnimator.SetTrigger("attack");
    63. rb2d.velocity=Vector2.zero;
    64. }
    65. }
    66.  
    67. privatevoidHandleInput()
    68. {
    69. if(Input.GetKeyDown(KeyCode.LeftShift))
    70. {
    71. attack=true;
    72. }
    73. }
    74.  
    75. privatevoidFlip(floathorizontal)
    76. {
    77. if(horizontal>0&& !facingRight || horizontal<0&&facingRight)
    78. {
    79. facingRight= !facingRight;
    80.  
    81. Vector3theScale=transform.localScale;
    82.  
    83. theScale.x*=-1;
    84.  
    85. transform.localScale=theScale;
    86. }
    87. }
    88.  
    89. privatevoidResetValues()
    90. {
    91. attack=false;
    92. }
    93. }
    94.  
     
  2. JaimieVos

    JaimieVos

    Joined:
    Jun 21, 2015
    Posts:
    193
    I really need the solution D:.
     
  3. CrystalDynamo

    CrystalDynamo

    Joined:
    May 22, 2014
    Posts:
    120
    I am thinking your attack animation has the Y coordinate or coordinates in it and when the animation runs it runs at the location it was made. I had a same issue with scores displaying and moving up the screen. I ended up handling the Y axis myself bit by bit in my update function. If there is no Y axis or X axis change in your animation remove the keys for them from your animation.

    Right click and delete the transform coordinates. Just removing Y won't do it either that appears to be a bug I found.

    Summary: Make sure there are no y coordinates present in your animation.

    Also you may want to use root motion on your animation if you don't. Check it out.

    If you have any questions about what I mean feel free to ask.
     
    Last edited: Apr 3, 2016
  4. JaimieVos

    JaimieVos

    Joined:
    Jun 21, 2015
    Posts:
    193
    I don't see anywhere y coordinates?
     
  5. CrystalDynamo

    CrystalDynamo

    Joined:
    May 22, 2014
    Posts:
    120
    So in your Animation window on the left you don't have the "Transform" property listed for your attack animation ?

    Transform stores position, rotation and scale.
     
  6. JaimieVos

    JaimieVos

    Joined:
    Jun 21, 2015
    Posts:
    193
  7. JaimieVos

    JaimieVos

    Joined:
    Jun 21, 2015
    Posts:
    193
    http://imgur.com/CuZ2sqo
     
  8. CrystalDynamo

    CrystalDynamo

    Joined:
    May 22, 2014
    Posts:
    120
    The Animation Window not the Animator window.

    Click the Window menu option up the very top and select animation.

    Make sure your object is selected in your scene that has the animation controller attached to see the animation.
     
  9. JaimieVos

    JaimieVos

    Joined:
    Jun 21, 2015
    Posts:
    193
    http://imgur.com/lLF8ba4 I select him and then click on animation but he doesn't show it in it.
     
  10. CrystalDynamo

    CrystalDynamo

    Joined:
    May 22, 2014
    Posts:
    120
    Ok thats not showing anything is your hero selected that has the animation attached in your scene ?

    It has to be the one in your scene not an asset.

    One that appears in your hierarchy list for your scene.

    If you dynamically instantiate your hero then just drag your asset onto the scene so you can click on it in the hierarchy list for your scene then we can see your animation. Its pain I know.
     
    Last edited: Apr 3, 2016
  11. CrystalDynamo

    CrystalDynamo

    Joined:
    May 22, 2014
    Posts:
    120
    He won't show in the animation window.

    The Animation window shows you the timeline of your animation and the attributes that change along the way.

    So first you MUST select him in your scene. If he is not there drag your asset and put him there and click on him. It won't show by just clicking the asset.

    Then look at the animation window. On the left just below the play button click the drop down and select the attack animation. Then that will list underneath it what is changed over the time of your animation. The time line is to the right.

    That list is where we do not want to see Transform.

    If it is there right click and remove it.

    Remove the hero from your scene if you had to drag it onto your scene to click on to see the animation screen populated.

    Run the scene again and it should be fixed if you removed it.
     
    Last edited: Apr 3, 2016
  12. JaimieVos

    JaimieVos

    Joined:
    Jun 21, 2015
    Posts:
    193
    http://imgur.com/ledVtQJ
     
  13. CrystalDynamo

    CrystalDynamo

    Joined:
    May 22, 2014
    Posts:
    120
    Excellent, click the little arrow next to "HERO : Sprite" to list the properties it is storing in the Animation for the sprite.
     
  14. JaimieVos

    JaimieVos

    Joined:
    Jun 21, 2015
    Posts:
    193
    THX BRO YOU FIXED IT!
    I looked at it and i saw blue points every point was 1 frame and at 1 frame he goes into the ground. I deleted that frame and now he plays fine!
     
  15. CrystalDynamo

    CrystalDynamo

    Joined:
    May 22, 2014
    Posts:
    120
    Awesome , I only know because I have had stuff like that happen to me.

    Show us a new fixed animation like the OP if you get time. That would be cool to see.
     
  16. JaimieVos

    JaimieVos

    Joined:
    Jun 21, 2015
    Posts:
    193
    Soon i post the entire game ;).
     
  17. CrystalDynamo

    CrystalDynamo

    Joined:
    May 22, 2014
    Posts:
    120
    Cool , let me know when you do.
     
  18. JaimieVos

    JaimieVos

    Joined:
    Jun 21, 2015
    Posts:
    193
  19. JaimieVos

    JaimieVos

    Joined:
    Jun 21, 2015
    Posts:
    193