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 · Dec 21, 2014 at 07:56 AM · animationjavascripttimecombo

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

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

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:

  1. Get recording string, to record inputs

  2. run a timer that resets after each input

  3. when timer reaches zero, use recording string to invoke your desired animation

  4. Clear the string ready for the characters next combo sequence

WARNING! : CODE REMAINS UNTESTED!

Hope it helps

Comment
Add comment · Show 6 · 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 · Dec 22, 2014 at 07:54 AM 0
Share

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?

avatar image Fornoreason1000 · Dec 22, 2014 at 07:59 AM 0
Share

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

avatar image IKilledKenny_2 · Dec 22, 2014 at 01:07 PM 0
Share

Oh Ok...I'll give it a go.Thank You So $$anonymous$$uch!!!

avatar image IKilledKenny_2 · Dec 31, 2014 at 11:01 PM 0
Share
 #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?

avatar image Fornoreason1000 · Jan 01, 2015 at 11:29 AM 0
Share

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.

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

27 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to save and display players times using Playerprefs in Javascript? 1 Answer

Climb animation is glitchy and isnt working properly 0 Answers

Animation.Time help 1 Answer

Shooting length 1 Answer

How would I go about carrying out another object's animation IN C#! 1 Answer


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