Having a hard time having consecutive animations for an attack

Posted by Kelby Styler on Game Development See other posts from Game Development or by Kelby Styler
Published on 2014-08-24T01:13:05Z Indexed on 2014/08/24 10:32 UTC
Read the original article Hit count: 431

Filed under:
|
|
|

So I've been trying to figure this out for about 8 hours now...It's driving me nuts because I am pretty sure that it is something dead simple that I am just not understanding. I had everything working fine when I was just cycling through the animation: Idle -> Attack -> Attack 1 -> Attack 2. Just in an infinite loop.

The problem now is that I want it to go Attack -> check if x time passes if ctrl pressed before x passes move to Attack 1, if not move back to Idle -> Then either Attack 1 or Idle depending on how long has passed.

I've almost gotten it a few time, but something always happens where it falls apart if I press ctrl too fast or after multiple cycles of the animation.

Any help would be appreciated, I'm just at my wits end on this one. I've been looking at this so long that I just don't know where to go anymore.

Code is below, here is the controller

using UnityEngine;
using System.Collections;

public class MeleeAttack : MonoBehaviour
{

public int damage;
public bool Attack;
public bool Attack1;
public bool Attack2;
public bool Idle;
private Animator animator;
private int attnum = 0;
private float count = 2f;
private float timeLeft;

//Gives value to damage output
void MAttackDmg ()
{

    if (Input.GetKeyDown (KeyCode.RightControl) || Input.GetKeyDown (KeyCode.LeftControl)) {

        switch (attnum) {
        case (0):
            Attack = true;
            damage = 2;
            animator.SetBool ("Attack", Attack);
            attnum++;
            Idle = false;
            animator.SetBool ("Idle", Idle);
            timeLeft = count;
            break;
        case (1):
            Attack1 = true;
            damage = 2;
            animator.SetBool ("Attack1", Attack1);
            attnum++;
            Idle = false;
            animator.SetBool ("Idle", Idle);
            timeLeft = count;
            break;
        case (2):
            Attack2 = true;
            damage = 2;
            animator.SetBool ("Attack2", Attack2);
            attnum = 0;
            Idle = false;
            animator.SetBool ("Idle", Idle);
            timeLeft = count;
            break;
        }
    }

    if (Input.GetKeyUp (KeyCode.RightControl) || Input.GetKeyUp (KeyCode.LeftControl)) {


        switch (attnum) {
        case (0):
            Debug.Log ("false");


            damage = 0;
            if (timeLeft <= 0f) {
                Attack2 = false;
                animator.SetBool ("Attack2", Attack2);
                Debug.Log ("t1");
                Idle = true;
                animator.SetBool ("Idle", Idle);
                attnum = 0;
                timeLeft = count;
            }
            break;
        case (1):
            Debug.Log ("false1");


            damage = 0;
            if (timeLeft <= 0f) {
                Debug.Log ("t2");
                Attack = false;
                animator.SetBool ("Attack", Attack);
                Idle = true;
                animator.SetBool ("Idle", Idle);
                attnum = 0;
                timeLeft = count;
            }
            break;
        case (2):
            Debug.Log ("false2");

            damage = 0;
            if (timeLeft <= 0f) {
                Attack1 = false;
                animator.SetBool ("Attack1", Attack1);
                Debug.Log ("t3");
                Idle = true;
                animator.SetBool ("Idle", Idle);
                attnum = 0;
                timeLeft = count;
            }
            break;  
        }
    }
}


// Use this for initialization
void Awake ()
{
    animator = GetComponent<Animator> ();
}


// Update is called once per frame
void Update ()
{
    timeLeft -= Time.deltaTime;;
    MAttackDmg ();
}

void Start (){
    timeLeft = count;
}

}

© Game Development or respective owner

Related posts about c#

Related posts about unity