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): using UnityEngine; using System.Collections; publicclassPlayer:MonoBehaviour{ privateRigidbody2Drb2d; privateAnimatormyAnimator; [SerializeField] privatefloatmovementSpeed; privateboolattack; privateboolfacingRight; publicGameObjectrigidbody; voidStart() { facingRight=true; rb2d=rigidbody.GetComponent<Rigidbody2D>(); myAnimator=GetComponent<Animator>(); } voidUpdate() { HandleInput(); } voidFixedUpdate() { floathorizontal=Input.GetAxis("Horizontal"); HandleMovement(horizontal); Flip(horizontal); HandleAttacks(); ResetValues(); } privatevoidHandleMovement(floathorizontal) { if(!this.myAnimator.GetCurrentAnimatorStateInfo(0).IsTag("Attack")) { rb2d.velocity=newVector2(horizontal*movementSpeed,rb2d.velocity.y); } rb2d.velocity=newVector2(horizontal*movementSpeed,rb2d.velocity.y); myAnimator.SetFloat("speed",Mathf.Abs(horizontal)); } privatevoidHandleAttacks() { if(attack&& !this.myAnimator.GetCurrentAnimatorStateInfo(0).IsTag("Attack")) { myAnimator.SetTrigger("attack"); rb2d.velocity=Vector2.zero; } } privatevoidHandleInput() { if(Input.GetKeyDown(KeyCode.LeftShift)) { attack=true; } } privatevoidFlip(floathorizontal) { if(horizontal>0&& !facingRight || horizontal<0&&facingRight) { facingRight= !facingRight; Vector3theScale=transform.localScale; theScale.x*=-1; transform.localScale=theScale; } } privatevoidResetValues() { attack=false; } }
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.
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.
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.
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.
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.
Excellent, click the little arrow next to "HERO : Sprite" to list the properties it is storing in the Animation for the sprite.
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!
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.
I will do . Im with 1 problem left: http://forum.unity3d.com/threads/enemy-health.395986/#post-2584893
The game is ready ! I made 1 more game after this 1 was finished. http://forum.unity3d.com/threads/elysem-demo-pc.397373/ and http://forum.unity3d.com/threads/cookie-family-pc.397374/