3
\$\begingroup\$

enter image description hereI 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);
 }
} 
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – user199845 Jul 21 '16 at 14:09
  • \$\begingroup\$ 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. \$\endgroup\$ – McAden May 3 '17 at 0:24
1
\$\begingroup\$

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);
        }
    }


} 
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – user199845 Jul 21 '16 at 13:47
  • \$\begingroup\$ Edited answer. Put collision code please. \$\endgroup\$ – klaymen Jul 21 '16 at 14:07
  • \$\begingroup\$ What does collision have to do with the animation? \$\endgroup\$ – user199845 Jul 21 '16 at 14:09
  • \$\begingroup\$ That collider2d object made me confused. Anyway I think the problem is in animation and animator components. This code don't play animation twice. \$\endgroup\$ – klaymen Jul 21 '16 at 14:18
  • \$\begingroup\$ 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 \$\endgroup\$ – user199845 Jul 21 '16 at 14:20
1
\$\begingroup\$

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: Animation Wrap Mode

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.