Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
1
Question by PrimeDerektive · Apr 28, 2010 at 07:10 PM · animationjavascript-specific

Resetting a combo attack chain if next attack did not occur in time

So I'm trying to make my melee attack script so that there are three different attack animations, and if you chain 3 attacks (left mouse clicks) within a certain amount of seconds between each, it will play the different animations for the different attacks.

I have it working so that an attackCounter is initialized, and every time the fire button is pressed it is incremented, will play a different animation depending on its value, and reset at 3. Also, every third swing I add another delay(comboDelay) to the delay between swings, so you can only do 3 attacks in a chain before a brief pause. However, I cannot figure out how to make it only increment and play a different animation if the next button press was within the correct timeframe (we'll call it timeAllowedBetweenCombo).

Here is the script in its current incarnation:

var swingRate = 0.25; var comboDelay = 0.5;

private var nextSwing = 0.0; private var swingCounter = 0;

function Update () {

 if(Input.GetButtonDown("Fire1")){

     if (Time.time > nextSwing){

         switch(swingCounter){

             case 0:
                 //This is the first swing in the chain
                 nextSwing = Time.time + swingRate;
                 break;

             case 1:
                 //This is the second swing in the chain
                 nextSwing = Time.time + swingRate;
                 break;

             case 2:
                 //This is the last swing in the chain
                 nextSwing = Time.time + swingRate + comboDelay;
                 break;

         }

         swingCounter++;

     }

 }

 //reset combo counter every third swing
 if(swingCounter == 3){
     swingCounter = 0;
 }

}

I've tried adding a variable called lastSwing and setting it equal to Time.time at the end of If(GetButtonDown("Fire1")){, and then checking if Time.time > lastSwing+timeAllowedBetweenCombo inside each of the switch cases, but to no avail :(

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
3

Answer by Jason_DB · Apr 28, 2010 at 08:57 PM

What I would do is change the if from Time.time > nextSwing to being within a certain difference from the target time.

//comboTime is how close you have to be to the correct time in order to combo
if(Mathf.Abs(Time.time - nextSwing) < comboTime)

So what you could do is put what you have now in that if, but add an }else{ which resets the combo.

if (Mathf.Abs(Time.time - nextSwing) < comboTime){

         switch(swingCounter){

             case 0:
                 //This is the first swing in the chain
                 nextSwing = Time.time + swingRate;
                 break;

             case 1:
                 //This is the second swing in the chain
                 nextSwing = Time.time + swingRate;
                 break;

             case 2:
                 //This is the last swing in the chain
                 nextSwing = Time.time + swingRate + comboDelay;
                 break;

         }

     } else {
          swingCounter = 0;
     }

Comment
Add comment · Show 1 · 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 PrimeDerektive · Apr 29, 2010 at 12:15 AM 0
Share

What is the significance of the $$anonymous$$ath.Abs part? Also, I can't tell but it seems that in your example, if the swing was not within the time of the last combo swing, there will be no swing at all (rather than resetting and starting over?) I can't really tell, thrown off by the time calculation. Why is time so confusing? :(

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

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Animation script with is.grounded help 0 Answers

change animation name in inspector 2 Answers

Animation component in Unity 4.3 doesn't work? 2 Answers

Character controller problem? 2 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