[HELP]2D character messing up attack animations on mobile

Ok so I’m working on a 2d platformer with a random melee attacks system. Currently the animator works like this:

There are 3 attack animations. You can go from idle to any one of them depending on random.range in the script, and you can return to idle. You can go from run to any attack but you can’t return to run without going to idle first. The player is supposed to attack when you get the attack axis from cross-platform input manager (yes, it’s a mobile game). When you get that axis, you set the trigger for any one of the three attack animations depending on what random.range returns and movement is stopped by setting the rigidbody2d’s velocity to 0

Well, the problem is that when I play the game on my multi touch android device, and I hold the left or right movement button and at the same time, keep tapping the attack button, the character runs, stops and attacks and the cycle repeats. That’s perfectly fine, but when you stop pressing the buttons after your chaotic button presses, the character repeats a max of 3 attack animations by itself!

I have tried like crazy to fix it up but I can’t come up with anything! So here’s the controller script: http://Pastebin.com/vzuv5dLB

Looking forward to some help.

Hi @Android_Apocalypse, I will not profess to being an expert here, not by any means, but a couple of things spring to mind.

Firstly, the behaviour you describe sounds a lot like when you hold a key down on the keyboard and then watch, say Word, try to catch up… you lift your finger but sometimes a few characters then appear… in your case, the animations being triggered those few extra times, but that would probably be too easy! If it was that I was going to suggest adding a check to see if the animation was already animating, and isAttack = true, then don’t do anything, or more tidily, perhaps it’s inverse.

The other thing I’ve noticed is that you are using both Update() and FixedUpdate(). I’ve not used the latter myself before, but the documentation suggests that this should be used if you are dealing with RigidBody, and I wondered if maybe you are seeing the effects of using both - although, you are only really setting a variable to be true in the Update() method, would it make any significant different to move that code to the FixedUpdate() method instead?

You could also change that test to just say;
isAttacking = CrossPlatformInputManager.GetButtonDown ("Attack")

… as the GetButtonDown() method is returning a Boolean anyway.

From what I can tell with the FixedUpdate() method you have, if you aren’t attacking, e.g isAttacking = false, you then go on to call the three methods which handle the attacking sequence anyway? So could the fact that it repeats this up to three times is just because you happen to move your character after the initial attack?

What would happen if you changed the code to something like this;

// void Update(){
//    if(CrossPlatformInputManager.GetButtonDown ("Attack")){
//        isAttacking = true;
//    }
// }

// Update is called once per frame
void FixedUpdate () {
    float horizontal = CrossPlatformInputManager.GetAxis ("Horizontal");
    isAttacking = CrossPlatformInputManager.GetButtonDown ("Attack");
   
    if(isAttacking == false){
        MoveControl (horizontal);    // move if we are not attacking
    } else {                                     // if isAttacking = true, attack!
        Flip (horizontal);
        HandleAttack ();
        Reset ();
    }
}

…again this is your code and I am not obviously familiar with everything you may have going on… hope this may help at least steer you in a direction to working it out yourself if my code changes above are non-sense :slight_smile:

1 Like

Definitely blowing some sense into my mind here. I will make these changes and let’s hope it works out. Thank you for your amazing reply :slight_smile:

1 Like

You’re welcome @Android_Apocalypse, just hope it makes a difference… hard to tell from this end etc… :slight_smile:

Hey @Android_Apocalypse, did you manage to resolve the problem? Can this topic be marked as [Solved] now?

Privacy & Terms