# Unity 2D animation issue

I have this weird animation bug in Unity that I believe is how Unity handles the frame. I dont know how to show the image so I'll do my best to explain it. So I put a bunch of 2D sprites in a sequential order right and I made a timer in my code to play animation whenever. The weird thing is that it does complete the whole animation but then it goes back to the first frame and ends it. I tried changing the timer to every number possible but it just does the same thing or repeats the animation twice when I increase my timer.

Here's the code :

using UnityEngine;
using System.Collections;

public class PlayerAttack : MonoBehaviour {

private bool attacking = false;//Switches for all attac combo
private bool attacking2 = false;
private bool attacking3 = false;

//attack 1 stats
public float attackTimer = 0f;
public float attackCD = .3f;

public Collider2D attackTrigger;//Referring to our attack collider

Animator anim;

// Use this for initialization
void Start () {
anim = GetComponent<Animator> ();
attackTrigger.enabled = false;//Disable our collider at start
}

// Update is called once per frame
void Update () {
if (Input.GetKeyDown ("space") && !attacking)
{
attacking = true;//We are attacking
attackTimer = attackCD;//assign variables

attackTrigger.enabled = true;//Enabled our trigger collider
}

//If we are attacking
if (attacking) {
//Check if our timer is greater than zero
if(attackTimer > 0){
attackTimer -= Time.deltaTime;//Decrease our timer
}else
{
attacking = false;//We arent attacking anymore
attackTrigger.enabled = false;
}
}
anim.SetBool ("Attack", attacking);
}
}

• How can I do that? I was going to in the first place but I didnt know where to get the image and place it on the forums. – user199845 Jul 21 '16 at 14:09
• I thought I understood the question but after I answer it I think I may have been mistaken. It sounded originally like you want your animation to go to the end and then stop (instead of resetting to first frame) but it seems instead you're wanting to transition to idle or run instead? If so we'll need to see the triggers on your transitions. – McAden May 3 '17 at 0:24

There's no need to attacking variable. it should work.

public class PlayerAttack : MonoBehaviour {

private bool attacking = false;

public float attackTimer = 0f;
public float attackCD = .3f;

public Collider2D attackTrigger;
Animator anim;

// Use this for initialization
void Start () {
anim = GetComponent<Animator> ();
attackTrigger.enabled = false;
attackTimer = attackCD;//       assign variables
}

// Update is called once per frame
void Update () {
if (Input.GetKeyDown ("space"))
{
anim.SetBool ("Attack", true);

attackTrigger.enabled = true;
if(attackTimer > 0){
attackTimer -= Time.deltaTime;//Decrease our timer
}else
{
attackTrigger.enabled = false;
anim.SetBool ("Attack", false);
}
}

}

• It's still going to the first frame then ending after it played the full animation and I dont know if it's how Unity is handling the animation or if it's in the code. Btw the if statement with the attackTimer had to be outside of the button input statement. – user199845 Jul 21 '16 at 13:47
• Edited answer. Put collision code please. – klaymen Jul 21 '16 at 14:07
• What does collision have to do with the animation? – user199845 Jul 21 '16 at 14:09
• That collider2d object made me confused. Anyway I think the problem is in animation and animator components. This code don't play animation twice. – klaymen Jul 21 '16 at 14:18
• Here's the thing, it plays the animation once, just like I wanted to but then it all of a sudden goes to the first frame of the animation then stops. The code should be fine i just dont understand why it's doing that. There's no other factor ik that is doing that :L – user199845 Jul 21 '16 at 14:20

I believe what you're looking for is the animation's WrapMode. Go to the inspector for the Animation file. Click the menu icon next to the "padlock" icon in the very very top-right corner. Change it to Debug and you should see "Wrap Mode" as highlighted here: