- Home /
Attack combo
I have been researching a lot on attack animations and scripting combos. I have used the following code that I've found online but I had a few questions about it, if anyone has to time to clarify.
Code Sample:
if (Input.GetButtonDown("Punch") && isGrabbing == false)
{
LastPress = Time.timeSinceLevelLoad;
if(tap > 2)
{
tap = 1;
}
else
{
tap++;
}
Combo2();
}
if(timer - LastPress > 1)
{
tap = 0;
if(timer - LastPress < Atime)
{
attack = false;
}
else
{
attack = true;
}
}
I do not fully understand how to control the key presses. When I press my Punch button, the animation is constantly interrupted because there is not delay between key press.
The following code is a switch statement that I'm using with Unity's Animator Control because I'm using Unity2D, which does not allow "legacy animations"
Switch Statement:
void Combo ()
{
//WaitForSeconds(2);
if(attack)
{
switch (tap)
{
case 0:
//Reset to Idle
anim.Play("FightStance");
break;
case 1:
//Combo Start
anim.Play("punch1 1");
break;
case 2:
//Combo Prolonge
anim.Play("punch2");
break;
case 3:
//Combo Finished
anim.Play("kick1");
break;
}
}
}
Sorry for all the detail but my main question is: How can I allow the animator to play my full attack animation before I press the Punch button to perform the next animation. Also, is it possible to force my pressing of the Punch button be inactive until the animator finish playing my animation state.
Thanks in advance to anyone who can help me with this Unity2D puzzle.
Answer by yashpal · Nov 04, 2014 at 08:27 AM
Hello @KetihG28, There are many ways you can perform this task.
1)Playing two separate animations one after another upon completion
2) You can make Animation Events in your animation. Put Animation event at the end Of clip. Next, In your script you make one bool which indicate animation is playing or not(I call it IsComboAnimationPlaying). And IsComboAnimationPlaying = true
when you play your animation. and make one function which is set false IsComboAnimationPlaying. like
void AnimationIsOver()
{
IsComboAnimationPlaying = false;
}
And Give function named "AnimationIsOver" in your your Animation Events. And Don't call next animation while IsComboAnimationPlaying == true So Basically Animation event call AnimationIsOver when animation is reach to point where you put animation event in animation clip.
3) You can same way disable Punch Button by
PunchButtonGameObject.SetActive (false);
PunchButtonGameObject.SetActive (true);
And Many more ways. Hope This will help you.
Answer by KetihG28 · Nov 06, 2014 at 04:59 AM
I have resolved the issue! Thankx yashpal! I'm not a beginner with Unity but I'm not a pro either. I didn't know anything about Animation events but I knew that would be the ticket to solving my issue. Here's a snippet of my code:(1st Code Snippet) void Combo () { if(attack) { switch (tap) {
case 0:
//Reset to Idle
anim.Play("FightStance");
break;
case 1:
//Combo Start
anim.Play("punch1 1");
IsComboAnimationPlaying = true;
break;
case 2:
//Combo Prolonge
anim.Play("punch2");
IsComboAnimationPlaying = true;
break;
case 3:
//Combo Finished
anim.Play("kick1");
IsComboAnimationPlaying = true;
break;
}
}
}
You suggested IsComboAnimationPlaying to be my condition to control what happens before and after an animation.
I also input this snippet:(2nd Code Snippet)
void AnimationIsOver() { IsComboAnimationPlaying = false;
Debug.Log ("Animation has finished"); <--- Not required just checking to see if the function is being acknowledged
}
So in order for IsComboAnimationPlaying to be most effective, I added an Animation event at the end of each of my attacks.
Resolution in order: 1)bool IsComboAnimationPlaying set to false
2)Have the 2nd snippet of code that I've shown, be the off switch for IsComboAnimationPlaying.
3) I set IsComboAnimationPlaying = true when I performed each animation (On switch) (1st code snippet)
4) After each animation has played I set IsComboAnimationPlaying = false (Off switch)
The major thing that controlled this so well was my "tap" commands that work through the animations. I set the condition to that, so that I couldn't constantly pressed my Punch button and increment the tap value.
Code Snippet:
timer = Time.timeSinceLevelLoad;
//if(Input.GetKeyDown(KeyCode.F))
if (IsComboAnimationPlaying == false) <------ Very important to control tap value { Debug.Log("Attack is allowed");
if (Input.GetButtonDown ("Punch"))
{
LastPress = Time.timeSinceLevelLoad;
if (tap > 2)
{
tap = 1;
}
else
{
tap++;
}
Combo ();
}
if (timer - LastPress > 1)
{
tap = 0;
if (timer - LastPress < Atime)
{
attack = false;
}
else
{
attack = true;
}
}
}
Sorry for the long explanation, I'm obviously new to forums but I just wanted to give a shot out to yashpal and also spread the wealth of knowledge I've gain. Thanks again.
@ $$anonymous$$etihG28
what did your completed script look like, and did it completely achieve what you set out to when you asked this question?
Answer by Dr_GeoFry · Nov 02, 2018 at 08:28 PM
@ KetihG28
what did your completed script look like, and did it completely achieve what you set out to when you asked this question?