Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by IKilledKenny_2 · Feb 04, 2015 at 02:07 AM · javascriptstringcombo

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 = "";
  
  }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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 = "";
       
       }

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IKilledKenny_2 · Feb 04, 2015 at 05:42 PM 0
Share
 #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 = "";
  
  }
  
avatar image IKilledKenny_2 · Feb 04, 2015 at 05:42 PM 0
Share

Like that??Im sorry,Im trying my best to understand this....

avatar image yashpal · Feb 07, 2015 at 04:47 AM 0
Share

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.

avatar image IKilledKenny_2 · Feb 07, 2015 at 06:41 PM 0
Share

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.

avatar image yashpal · Feb 16, 2015 at 05:06 AM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Combo Script Problem 1 Answer

get all text between two chars (js) 1 Answer

Access a GameObject through it's tag, using a string. 1 Answer

assigned function declaration in unity javascript for different types 1 Answer

Prototypal vs Classical-Inheritance UnityScript 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges