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
0
Question by monkaS98 · Jun 28, 2018 at 10:13 PM · attacktimer countdowncombo

Issue with timer in combo attack

Hello,

I am trying to create a simple combo system for my game. I have a script that almost works but I have an issue with combo attack timer. So the problem is that inside my AttackAnimation() function I start the timer by setting activateTimerToReset to true and then I increment currentComboState to perform next combo attack. However inside the ResetComboState () my currentComboState is also incremented so instead of easyCombo [0] I have easyCombo [1] and so on, which means that function starts the timer of the wrong combo hit and also it doesn't reset the timer. Is there any way to solve this issue?

Here's my script so far:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class AttackSystem : MonoBehaviour {

 public AttackCharacteristics[] easyCombo = new AttackCharacteristics[3];

 private float originalTimer;
 private int currentComboState = 0;
 bool activateTimerToReset = false;
 Animator anim;

 void Start () 
 {
     originalTimer = easyCombo [currentComboState].comboMaxTime;
     anim = GetComponentInChildren <Animator> ();
 }

 void Update ()
 {
     if (Input.GetKeyDown (KeyCode.A))
         AttackAnimation();
     
     ResetComboState (activateTimerToReset);
 }
     
 void AttackAnimation()
 {
     switch (currentComboState)
     {
     case 0:
         activateTimerToReset = true;
         anim.SetTrigger(easyCombo[currentComboState].animTriger);
         currentComboState++;
         Debug.Log ("1 hit.");
         break;
     case 1:
         anim.SetTrigger(easyCombo[currentComboState].animTriger);
         currentComboState++;
         Debug.Log ("2 hit");
         break;
     case 2:
         anim.SetTrigger(easyCombo[currentComboState].animTriger);
         Debug.Log ("3 hit!");
         currentComboState = 0;
         break;
     }
 }

 void ResetComboState (bool _timerToReset)
 {
     if (_timerToReset) 
     {
         easyCombo [currentComboState].comboMaxTime -= Time.deltaTime;

         if(easyCombo [currentComboState].comboMaxTime <=0)
         {
             currentComboState = 0;
             activateTimerToReset = false;
             easyCombo [currentComboState].comboMaxTime = originalTimer;
             anim.SetTrigger("BreakCombo");
         }
     }
 }

}

[System.Serializable]

public class AttackCharacteristics

{

 public string animTriger;
 public float comboMaxTime;

}

Comment
Add comment · Show 2
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 MotherChurchill · Jun 29, 2018 at 03:29 AM 0
Share

Two things from reading your code:

  1. I see you resetting the current easyCombo timer in ResetComboState, but only if the timer runs out. I don't see where you reset it if you press the key in time. $$anonymous$$aybe you should reset the timer every time you switch to a new combo state?

  2. I also noticed that ResetComboState only takes one argument, and does nothing when the argument is false. Rather than check for this in the function itself, I would check this condition during the update loop, and only call the function if activateTimerToReset == true. It avoids the overhead of loading a function every frame if it doesn't have to. Won't make a serious performance difference, but I thought I'd point it out.

Let me know if I'm misunderstanding your code.

avatar image monkaS98 MotherChurchill · Jun 29, 2018 at 11:29 AM 0
Share

First of all thanks for your reply, I will rewrite my code according to your advises. However the main issue is that ResetComboState starts wrong timer for each case. For example I press the A button for the first time, so the case 0 should be executed, and it should looks like this:

case 0: activateTimerToReset = true; anim.SetTrigger(easyCombo[0].animTriger); currentComboState++; Debug.Log ("1 hit."); break;

But then, because case 0 increments currentComboState++, ResetComboState function executes this code:

void ResetComboState (bool _timerToReset) { if (_timerToReset) { easyCombo [1].combo$$anonymous$$axTime -= Time.deltaTime; if(easyCombo [1].combo$$anonymous$$axTime <=0) { currentComboState = 0; activateTimerToReset = false; easyCombo [1].combo$$anonymous$$axTime = originalTimer; anim.SetTrigger("BreakCombo"); } } }

Here's screenshot from the inspector to make it more clearer:

alt text

screenshot.jpg (104.8 kB)

1 Reply

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

Answer by madks13 · Jun 29, 2018 at 10:05 AM

Sorry, i fail to see where your currentComboState is incremented a second time. You set it to 0 in 2 places, but only 1 place (the switch) increments it.

Edit : below an exemple on autocycling :

     public class AttackSystem : MonoBehaviour
     {
         public AttackCharacteristics[] easyCombo = new AttackCharacteristics[3];
         public AttackCharacteristics currentCombo;
 
         private float timer;
         private int currentComboState;
         private bool activateTimerToReset = false;
         private Animator anim;
 
         void Start()
         {
             ChangeCombo(0);
             anim = GetComponentInChildren<Animator>();
         }
 
         void Update()
         {
             if (Input.GetKeyDown(KeyCode.A))
                 AttackAnimation();
 
             ResetComboState(activateTimerToReset);
         }
 
         void AttackAnimation()
         {
             if (currentComboState == 0)
             {
                 activateTimerToReset = true;
             }
             anim.SetTrigger(currentCombo.animTriger);
             Debug.Log((currentComboState + 1) + "hit.");                
             //Cycle trough elements of easyCombo
             ChangeCombo((currentComboState + 1) % easyCombo.Length);
         }
 
         void ResetComboState(bool _timerToReset)
         {
             if (_timerToReset)
             {
                 timer -= Time.deltaTime;
                 if (timer <= 0)
                 {
                     ChangeCombo(0);
                     activateTimerToReset = false;
                     anim.SetTrigger("BreakCombo");
                 }
             }
         }
 
         void ChangeCombo(int index)
         {
             currentComboState = index;
             currentCombo = easyCombo[index];
             timer = currentCombo.comboMaxTime;
         }
 
     }
Comment
Add comment · Show 5 · 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 monkaS98 · Jun 29, 2018 at 11:36 AM 0
Share

I increment currentComboState in each case. The problem occures in ResetComboState function.

For example I press the A button for the first time, so the case 0 should be executed, and it should looks like this:

case 0: activateTimerToReset = true;

anim.SetTrigger(easyCombo[0].animTriger);

currentComboState++;

Debug.Log ("1 hit."); break;

But then, because case 0 increments currentComboState++, ResetComboState function executes this code:

void ResetComboState (bool _timerToReset)

{ if (_timerToReset)

{ easyCombo [1].combo$$anonymous$$axTime -= Time.deltaTime;

if(easyCombo [1].combo$$anonymous$$axTime <=0)

{ currentComboState = 0;

activateTimerToReset = false;

easyCombo [1].combo$$anonymous$$axTime = originalTimer;

anim.SetTrigger("BreakCombo"); }

} }

Here's screenshot from the inspector to make it more clearer:alt text

screenshot.jpg (104.8 kB)
avatar image madks13 monkaS98 · Jun 29, 2018 at 02:38 PM 0
Share

I see,

then, why not simply get the ref to current combo state object and use that ins$$anonymous$$d of the index?

avatar image monkaS98 madks13 · Jun 29, 2018 at 06:45 PM 0
Share

Thanks for your reply and the code that you've posted above. However, the issue still persists - when I press A button 1 time, I see "2hit." in the console, and also Attack2 trigger activates, ins$$anonymous$$d of Attack1.

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

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

Related Questions

mecanim blend tree attack combo 2 Answers

Animations Combo 0 Answers

Resetting Combos 1 Answer

How do you script combo attacks? 2 Answers

Combo Attack Chain Script Help 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