- Home /
Combo Script Problem
Hello Unity3D.I have a problem with my combo script.Anytime i put my combo script into my character.My character freezes and is not able to do the combo or anything at all.If anyone knows why this problem occurs.Can anyone please tell me why?(I have been stuck on this for 2 weeks now....)
P.S Heres a script i have been trying to get with
var comboCount : int = 0;
var fireRate : float = 3;
var timer : float = 0;
var emptygo:GameObject;
var recordInput : String = "";
var waitClearSeconds : float = 1;
function Update()
{
RecordInput();
waitClearSeconds -= Time.deltatime;
if(waitClearSeconds < 0f) {
GetCombo();
Clear();
}
else
waitClearSeconds -= Time.deltaTime; // This is only reduce when it is grater than 0.
}
function RecordInput() {
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("Rex_Demon__Attack_1");
recordInput += "p";
waitClearSeconds = 1; //reset the wait time to avoid clearing too early
audio.Play(); //Finally play the audio
audio.volume = 50;
emptygo.GetComponent.<Rigidbody>().velocity = emptygo.transform.TransformDirection(Vector3.forward);
emptygo.rigidbody.AddForce(emptygo.transform.forward * 5);
}
else
{
fireRate -= Time.deltaTime;
comboCount++;
if (comboCount == 2)
{
Debug.Log("I did a combo!");
//Do something awesome for the combo!
animation.Play("Rex_Demon__Attack_2");
recordInput += "p";
waitClearSeconds = 1f; //reset the wait time to avoid clearing too early
}
else
{
comboCount++;
if (comboCount == 4)
{
Debug.Log("I did a combo!");
//Do something awesome for the combo!
animation.Play("Rex_s_Combo_Finish_2");
animation["Rex_s_Combo_Finish_2"].speed= 2;
recordInput += "p";
waitClearSeconds = 1f; //reset the wait time to avoid clearing too early
}
else
{
comboCount++;
if (comboCount == 6)
Debug.Log("I did a combo!");
//Do something awesome for the combo!
animation.Play("Rex_Demon__Attack_3");
GetCombo();
}
}
}
}
if (fired)
{
timer += Time.deltaTime;
if (timer > fireRate)
{
comboCount = 0;
fired = false;
}
}
}
function GetCombo() {
if((Input.GetKeyDown("k")&&recordInput == "ppp"))
{
animation.Play("Spinning_Slashes");
waitClearSeconds = 1f; //reset the wait time to avoid clearing too early
}
}
function Clear() {
recordInput = "";
}
Answer by yashpal · Feb 04, 2015 at 05:46 AM
hello @IKilledKenny_2,
Your script:
function Update() {
waitClearSeconds -= Time.deltaTime; // this is called every time so waitClearSeconds is become negative.
if(waitClearSeconds < 0f) {
RecordInput();
Clear();
}
}
changed Script function Update() {
if(waitClearSeconds < 0f) {
RecordInput();
Clear(); // Why use this.
}
else
{
waitClearSeconds -= Time.deltaTime; // This is only reduce when it is grater than 0.
}
}
And you use if wrong way. you use if(condition); //if you put semicolon at the end of if condition it will not do any thing if it true or false.
Edited:
Here is a code you might want. I use switch case for detect keypress for combo. and this might be not best way to do this. Tell me if you don't understand any thing in this code. (I didn't tested it. but you get basic idea from this.)
pragma strict
var recordInput : String = "";
var waitClearSeconds : float = 2; //wait 1 second after the last input before clear, you will have to adjust this setting later
var waitToDetectKeyPressTimeSeconds : float = 1; //This is smaller than waitClearSeconds.if waitToDetectKeyPressTimeSeconds == 1 and player press "k" at 10Sec and player press "k" again at 10.3Sec it will not detect it is only detect after 11Sec(10 +waitToDetectKeyPressTimeSeconds).
function Update()
{
if(waitToDetectKeyPressTimeSeconds < 0f)
{
RecordInput();
}
else
{
waitToDetectKeyPressTimeSeconds -= Time.deltaTime;
}
}
function RecordInput()
{
if(Input.GetKeyDown(KeyCode.K))
{
switch (recordInput)
{
case "":
recordInput = "k";
animation.Play("Rex_Sword_Kick");
CancelInvoke("ClearKeyPressRecord");
Invoke("ClearKeyPressRecord",waitClearSeconds);
waitToDetectKeyPressTimeSeconds =1;
break;
case "k":
recordInput = "kk";
animation.Play("Rex_Sword_Kick_2");
CancelInvoke("ClearKeyPressRecord");
Invoke("ClearKeyPressRecord",waitClearSeconds);
waitToDetectKeyPressTimeSeconds =1;
break;
case "kk":
recordInput = "kkk";
animation.Play("Rex_Seal_Combo");
CancelInvoke("ClearKeyPressRecord");
Invoke("ClearKeyPressRecord",waitClearSeconds);
waitToDetectKeyPressTimeSeconds =1;
break;
case "pp": // if player is press first two time p key and than press k key for combo.
recordInput = "ppk";
animation.Play("Combo_ppk");
CancelInvoke("ClearKeyPressRecord");
Invoke("ClearKeyPressRecord",waitClearSeconds);
waitToDetectKeyPressTimeSeconds =1;
break;
}
}
if(Input.GetKeyDown(KeyCode.P))
{
switch (recordInput)
{
case "":
recordInput = "p";
animation.Play("Simple_First_Punch"); //I think p is for punch
CancelInvoke("ClearKeyPressRecord");
Invoke("ClearKeyPressRecord",waitClearSeconds);
waitToDetectKeyPressTimeSeconds =1;
break;
case "p":
recordInput = "pp";
animation.Play("Combo_Second_Punch");
CancelInvoke("ClearKeyPressRecord");
Invoke("ClearKeyPressRecord",waitClearSeconds);
waitToDetectKeyPressTimeSeconds =1;
break;
case "pp":
recordInput = "ppp";
animation.Play("Combo_Third_Punch");
CancelInvoke("ClearKeyPressRecord");
Invoke("ClearKeyPressRecord",waitClearSeconds);
waitToDetectKeyPressTimeSeconds =1;
break;
case "ppp":
recordInput = "pppp";
animation.Play("Combo_pppp");
CancelInvoke("ClearKeyPressRecord");
Invoke("ClearKeyPressRecord",waitClearSeconds);
waitToDetectKeyPressTimeSeconds =1;
break;
case "kkk":
recordInput = "kkkp";
animation.Play("Spinning_Slashes");
CancelInvoke("ClearKeyPressRecord");
Invoke("ClearKeyPressRecord",waitClearSeconds);
waitToDetectKeyPressTimeSeconds =1;
break;
}
}
}
function ClearKeyPressRecord()
{
recordInput = "";
}
#pragma strict
var recordInput : String = "";
var waitClearSeconds : float = 1; //wait 1 second after the last input before clear, you will have to adjust this setting later
function Update() {
if(waitClearSeconds < 0f) {
RecordInput();
}
else
{
waitClearSeconds -= Time.deltaTime;
}
}
function RecordInput() {
if(Input.Get$$anonymous$$eyDown("k"))
{
animation.Play("Rex_Sword_$$anonymous$$ick");
recordInput += "k";
waitClearSeconds = 0.1f; //reset the wait time to avoid clearing too early
}
{
if(Input.Get$$anonymous$$eyDown("k"))
animation.Play("Rex_Sword_$$anonymous$$ick_2");
recordInput += "kk";
waitClearSeconds = 0.1f; //reset the wait time to avoid clearing too early
}
{
animation.Play("Rex_Seal_Combo");
recordInput += "kkk";
waitClearSeconds = 0.1f; //reset the wait time to avoid clearing too early
GetCombo();
}
}
function GetCombo() {
if(recordInput == "kkk")
if(Input.Get$$anonymous$$eyDown("p"))
{
animation.Play("Spinning_Slashes");
waitClearSeconds = 0.1f; //reset the wait time to avoid clearing too early
}
}
function Clear() {
recordInput = "";
}
Like that??Im sorry,Im trying my best to understand this....
hello @I$$anonymous$$illed$$anonymous$$enny_2,
I think you want to do something like this. when player(who play the game) press "k" button first time than it play 1st animation. before that animation over if player press again "k" key than play next animation , and again before that animation complete player again press "k" button play next animation.
I tried that before.But it made it worse Dx...Is there like a way that when i do the $$anonymous$$,$$anonymous$$,$$anonymous$$,P i can cancel an animation to make another animation play ins$$anonymous$$d?For example,If i press "P" after the third "$$anonymous$$" and its suppose to play "Spinning_Slashes"and make it stop playing the first animation to my P,P,P,P combo?
Also I uploaded a update on the script.
hello @I$$anonymous$$illed$$anonymous$$enny_2, sorry for late reply. Check out my edited answer and tell me if you find any problem.
Hope it help you.