- Home /
Finish One Combo
Hello Unity3D.I have a problem with my combo script.The problem is that i can't use more than one button in order to do another animation.For example i want to a 4 hit combo using "p","p","p","k" buttons.But the combo script only allows me to use "p".Whenever i trying using "k" button my 5 hit combo starts off with the first animation to the "k" button combo.I don't want that.I want to be able to do the "p","p","p","k" combo without it triggering my "k",k",k",k",k" button combo.If anyone knows how i can do this.Can you please tell me how?
Here's the script that i am having trouble with
var fired : boolean = false;
var comboCount : int = 0;
var fireRate : float = 3;
var timer : float = 0;
function Update()
{
if (Input.GetKeyDown("p"))
{
if (!fired)
{
fired = true;
timer = 0.0;
comboCount = 1;
Debug.Log("I served a punch!");
//Do something awesome to deliver a punch!
animation.Play("Overhead_slash");
audio.Play(); //Finally play the audio
audio.volume = 50;
}
else
{
comboCount++;
if (comboCount == 2)
{
Debug.Log("I did a combo!");
//Do something awesome for the combo!
animation.Play("Overhead_slash2");
}
else
{
comboCount++;
if (comboCount == 4)
if (Input.GetKeyDown("p"))
{
Debug.Log("I did a combo!");
//Do something awesome for the combo!
animation.Play("Overhead_slash");
}
else
{
comboCount++;
if (comboCount == 6)
if (Input.GetKeyDown("p"))
Debug.Log("I did a combo!");
//Do something awesome for the combo!
animation.Play("Spinning_Slashes");
animation["Spinning_Slashes"].speed= 2;
}
else
{
comboCount++;
if (comboCount == 8)
if (Input.GetKeyDown("k"))
Debug.Log("I did a combo!");
//Do something awesome for the combo!
animation.Play("Overhead_slash4");
animation["Overhead_slash4"].speed= 2;
}
}
}
}
if (fired)
{
timer += Time.deltaTime;
if (timer > fireRate)
{
comboCount = 0;
fired = false;
}
}
}
Answer by Fornoreason1000 · Dec 22, 2014 at 04:59 AM
Hi, stop killing kenny, Cartmans getting mad :P
instead of having a legion of else blocks and conditions based on the number of buttons pressed, i suggest you try making a button recording system using strings.
string recordInput = "";
basically you have a recording string, this string basically keeps track of your inputs... and if there is no input for a giving amount for time the string will clear itself. this is very important so you character will not continue acting out combos and you can proceed with new combos.
string recordInput = "";
float waitClearSeconds = 1f //wait 1 second adter the last input before clear, you will have to adjust this setting later
void Update() {
RecordInput();
waitClearSeconds -= Time.deltatime;
if(waitClearSeconds < 0f) {
Clear();
}
}
void RecordInput() {
Input.GetKeyDown("p") {
recordInput += "p"
waitClearSeconds = 1f //reset the wait time to avoid clearing too early
}
Input.GetKeyDown("k") {
recordInput += "k"
waitClearSeconds = 1f //reset the wait time to avoid clearing too early
}
}
void Clear() {
recordInput = "";
}
to keep track, every time you press "p" or "k" you recording string will add "p" or "k" to it. again after a certain time the string must clear itself. in order to maintain character control. lets say you do a 3 punch combo, you will press "p" 3 times. your recording string will add "p" to it 3 times becoming "ppp".
string recordInput = "";
float waitClearSeconds = 1f //wait 1 second adter the last input before clear, you will have to adjust this setting later
void Update() {
RecordInput();
waitClearSeconds -= Time.deltatime;
if(waitClearSeconds < 0f) {
GetCombo();
Clear();
}
}
void GetCombo() {
if(recordInput == "ppp") {
//do my combo code
}
}
after that your script will wait until just before clear, this way if you have a combo that has a "pppkkp", it won't be interred with the "ppp" combo. just before clearing the recording string search for a combo that matches "ppp"., from there you can finally trigger that actions that involve your combo. now finally after all that you can clear the string ready for the next combo :)
simplified:
Get recording string, to record inputs
run a timer that resets after each input
when timer reaches zero, use recording string to invoke your desired animation
Clear the string ready for the characters next combo sequence
WARNING! : CODE REMAINS UNTESTED!
Hope it helps
First Im glad someone got my I$$anonymous$$illed$$anonymous$$enny joke!.Second will this work with javascript?Third would this script be able to change my animations speed?
whether if its Unity script or C# it doesn't actually matter, they both have equal capabilities in unity. the code i provided is in C#.
As for animation speed no... the code i posted was just a rough example to make it clearer on what i was explaining. All it serves to do is apply the theoretical solution i was suggesting. you can definitely change your animation speed but you will need to add that in yourself. As well as any combos you want
Oh Ok...I'll give it a go.Thank You So $$anonymous$$uch!!!
#pragma strict
var recordInput = "";
var waitClearSeconds : float = 1; //wait 1 second adter the last input before clear, you will have to adjust this setting later
function Update() {
RecordInput();
waitClearSeconds -= Time.deltaTime;
if(waitClearSeconds < 0f) {
Clear();
}
}
function RecordInput() {
Input.Get$$anonymous$$eyDown("q"); {
animation.Play("Rex_Sword_$$anonymous$$ick");
recordInput += "p";
waitClearSeconds = 1; //reset the wait time to avoid clearing too early
}
Input.Get$$anonymous$$eyDown("k"); {
animation.Play("Rex_Sword_$$anonymous$$ick_2");
recordInput += "k";
waitClearSeconds = 1; //reset the wait time to avoid clearing too early
}
}
function Clear() {
recordInput = "";
}
I putted this code in javascript but it makes my character do nothing...do you know why is that?
As i said in my Answer, the code is Untested, I imported my code and found boatloads of errors, lots of missing semicolons and missing key words and brackets.
the Input.Get$$anonymous$$eyDown lines are meant to be in a "if" statement which is why your code isn't working, since it isn't checking for anything;
if (Input.Get$$anonymous$$eyDown("p"))
{
recordInput += "p";
waitClearSeconds = 1f; //reset the wait time to avoid clearing too early
}
if (Input.Get$$anonymous$$eyDown("k"))
{
recordInput += "k";
waitClearSeconds = 1f; //reset the wait time to avoid clearing too early
}
also you will find that 1 second is far too long of a delay for the combo.