- Home /
How do you script combo attacks?
I've looked at every single one of the questions on this topic, and NONE of the answers work. All I'm trying to do is make it so when the player presses the button for the axis labeled "Attack", the character in the game makes the first slash with the sword (which is an animation I have called "attack1"). Then the player must press the attack button again at the right time, and the character will do the 2nd slash (an animation called "attack2", and the same for the third. I don't want the player to be able to button-mash though. If they do that, or if they don't press the attack button soon enough, I need it to go back to the first attack animation. So many people have asked how to do this and have gotten answers, but none of them work for me, and it's extremely frustrating. I've seen so many different scripts for this, so I wouldn't even know how to START making my own. Can someone please help me out here?
Animation Script
function Start ()
{
var array = new Array (Vector3(0, 0, 0), Vector3(0, 0, 1));
array.Push(Vector3(0, 0, 2));
array.Push(Vector3(0, 0, 3));
var builtinArray : Vector3[] = array.ToBuiltin(Vector3);
var newarr = new Array (builtinArray);
print (newarr);
// Stop animations that are already playing
//(In case user forgot to disable play automatically)
animation.Stop();
}
function FixedUpdate () {
// Based on the key that is pressed,
// play the walk animation or the idle animation
if (Mathf.Abs(Input.GetAxis("Forward")) > 0.1)
animation.CrossFade("walk");
else
animation.CrossFade("stand2");
if(Input.GetAxis("Forward") && (Input.GetAxis("Run"))
)animation.CrossFade("run");
}
Also, here's the conrtrol script
var walkSpeed : float = 0.62;
var speed : float = 0.62;
var runSpeed : float = 2;
var rotateSpeed : float = 3;
function Start () {
var array = new Array (Vector3(0, 0, 0), Vector3(0, 0, 1));
array.Push(Vector3(0, 0, 2));
array.Push(Vector3(0, 0, 3));
var builtinArray : Vector3[] = array.ToBuiltin(Vector3);
var newarr = new Array (builtinArray);
print (newarr);
}
function FixedUpdate() {
var controller : CharacterController = GetComponent( CharacterController );
transform.Rotate( 0, Input.GetAxis( "Horizontal" ) * rotateSpeed, 0 );
var forward = transform.TransformDirection( Vector3.forward );
var curSpeed = walkSpeed * Input.GetAxis( "Vertical" );
controller.SimpleMove( forward * curSpeed );
if (Mathf.Abs(Input.GetAxis( "Run" )) && Input.GetAxis( "Forward" )
)controller.SimpleMove( forward * runSpeed );
}
@script RequireComponent( CharacterController );
Can someone please help me with this? I need to be able to attack...
Answer by save · Jul 12, 2012 at 09:01 AM
The easiest way of doing it is to have a class which contains the variables for the different attacks, a timer and a time span within that timer. Hope comments help, otherwise feel free to ask!
The time span could be visualized this way:
|------<===O===>------|
Where | is full time, < left stop, > right stop, and O is origin. Within left and right stop the player is able to advance in combos until full length of the combos array is reached where it resets.
#pragma strict
var combos : ComboSequence[] = new ComboSequence[3]; //Combo class. Fill this out in Inspector.
var comboMaxTime : float = 1.0; //How long a timer for combo lasts in seconds. Fill this out in Inspector.
var comboSpanTime : float = .25; //The span of when a combo can start. Fill this out in Inspector.
var comboMidPoint : float = .5; //The position within the maxtime of a combo the span is active. Fill this out in Inspector.
private var currentCombo : int = -1;
private var comboTimeout : float = .0;
function Update () {
if (Input.GetButtonDown("Attack")) Attack();
if (comboTimeout > 0) DecreaseTime();
}
function Attack () {
ComboTime(); //Do all the logic for timing before animation etc.
animation.Play(combos[currentCombo].comboAnimation); //Play the currentCombo combos' animation
audio.clip = combos[currentCombo].comboSound; //Set the audio clip to the currentCombo combos' audio
audio.Play(); //Finally play the audio
//Add logic for hitting enemies etc here!
}
function ComboTime () {
if (currentCombo<combos.Length-1 &&
comboTimeout>0 &&
comboTimeout>comboMidPoint-comboSpanTime &&
comboTimeout<comboMidPoint+comboSpanTime ||
currentCombo==-1) {
currentCombo++;
} else {
currentCombo = -1;
}
comboTimeout = comboMaxTime;
}
function DecreaseTime () {
comboTimeout-=1*Time.deltaTime;
}
class ComboSequence {
var comboName : String; //A name for current attack (might be of use later for tutorial or something else)
var comboAnimation : String; //The animation name for current attack
var comboSound : AudioClip; //The audio for current attack
}
Let me know if you encounter any errors as this isn't tested.
I'm running into a problem I ran into with another script for combo attacks. When I press the key to attack, my character's attack animation starts to play, but stops. So only about a third of it actually plays. I really appreciate you writing this script and trying to help me, so thank you for that. I'm just confused as to why my animations aren't playing like they're supposed to. When I preview them in the animation window, they work fine, so I know it's not a problem with the animations themselves.
It sounds like there might be some issue with blending and layers (if you use that) or some other animation interfering with your attacks. It's hard to tell though. Do you have any other animation scripts that could potentially abort the attack animations?
Well I have an idle animation... I'll edit my question to include the other animation script.
If someone finds this too complex to understand .. they can visit
http://u3d.as/t6n Its Basically a cheat code system but also supports what you want
Answer by The-Arc-Games · Jul 12, 2012 at 08:25 AM
The scripting needed for the combo sequence is normally complex but straightforward.
We can't offer you a routine, but the logic is usually like this:
1- setup a 'state machine'. You can easily do this through boolean variables, i.e.:
slash1 = true;
slash2 = false;
slash3 = false;
slash4 = false;
slashfinal = false;
attackstart = Time.realtimeSinceStartup;
there's many ways to do this (and this proposal is actually inelegant), but this should work.
2- create an 'attack' routine that's called after you press the attack button, there you calculate the elapsed time since 'attackstart', and compare the 'elapsedsinceattack' value with the 'requiredforslashx' value, using the slash booleans to 'advance' in state.
3- if the elapsed time since attackstart is in the interval to advance to the next slash, you change the slash booleans accordingly, so that for example your Update can call an animation, change damage dealt, etc.
Your answer
Follow this Question
Related Questions
Animations Combo 0 Answers
How to make player attack animation play through without holding down a key. 0 Answers
Combo Attack Chain Script Help 0 Answers
What is causing my attack animation to be in a different position from my idle/run animation? 0 Answers
mecanim blend tree attack combo 2 Answers