Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
1
Question by Hiiro27 · May 26, 2016 at 12:21 PM · buttonattackcombo

how to make combo attack, 1 button do 3 things

need help,, my question is how to make one button do 3 things, cause i want to make combo attack, just like most fighting game. When i press one time, the button do attack1 animation. When i press two time, the button do attack1 and attack2 animation. And so on. this my script :

     private Animator myanim;
 
     void Start ()
     {
         myanim = GetComponent<Animator>();
         myanim.SetBool ("Attack1", false);
         myanim.SetBool ("Attack2", false);
         myanim.SetBool ("Attack3", false);
     }
 
     public void Attack1()
     {
         myanim.SetBool ("Attack1", true);
         print("This Attack 1");
     }
 
     public void Attack2()
     {
         myanim.SetBool ("Attack2", true);
         print("This Attack 2");
     }
 
     public void Attack3()
     {
         myanim.SetBool ("Attack3", true);
         print("This Attack 3");
     }
 
 // This public void i'm gonna use for ui image button.
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

2 Replies

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

Answer by UsmanAbbasi · May 26, 2016 at 01:57 PM

 int noOfClicks = 0;
 //Time when last button was clicked
 float lastClickedTime = 0;

 //Delay between clicks for which clicks will be considered as combo
 float maxComboDelay = 1;
 void Update()
 {
     if(Time.time - lastClickedTime > maxComboDelay)
    {
       noOfClicks = 0;
    }
 }

 //Call on button click
 void OnClick()
 {
        //Record time of last button click
        lastClickedTime = Time.time;
        noOfClicks++;
        if( noOfClicks == 1)
       {
                  animator.SetBool("Attack1" , true);
       }

        //limit/clamp no of clicks between 0 and 3 because you have combo for 3 clicks
        noOfClicks =  Mathf.Clamp( noOfClicks , 0 , 3);
 }
 

After this you have to attach StateMachineBehaviour scripts to animation states in animator which will check when state is over.

 //Attach this script to Attack1 state in animator
 public class PlayerAttack1Behaviour : StateMachineBehaviour {
 
      // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
     //override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
     //
     //}
 
     // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
     //override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
     //
     //}
 
     // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
     override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) 
     {
         animator.SetBool ("Attack1" , false);\

            // Obviously "noOfClicks" is not directly accessible here so you have to make it public and get it reference somehow to use it here 
         if(  noOfClicks >= 2 )
                 {
                         animator.SetBool ("Attack2" , true);
                 }
     }
     // OnStateMove is called right after Animator.OnAnimatorMove(). Code that processes and affects root motion should be implemented here
     //override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
     //
     //}
 
     // OnStateIK is called right after Animator.OnAnimatorIK(). Code that sets up animation IK (inverse kinematics) should be implemented here.
     //override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
     //
     //}
 }

Similarly attach this one to Attack2 state in Animator:

 //Attach this script to Attack2 state in animator
  public class PlayerAttack2Behaviour : StateMachineBehaviour {
  
       // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
      //override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
      //
      //}
  
      // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
      //override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
      //
      //}
  
      // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
      override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) 
      {
          animator.SetBool ("Attack2" , false);
          if(  noOfClicks >= 3 )
                  {
                          animator.SetBool ("Attack3" , true);
                  }
      }
      // OnStateMove is called right after Animator.OnAnimatorMove(). Code that processes and affects root motion should be implemented here
      //override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
      //
      //}
  
      // OnStateIK is called right after Animator.OnAnimatorIK(). Code that sets up animation IK (inverse kinematics) should be implemented here.
      //override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
      //
      //}
  
  
  }
 

In case you don't know about StatemachineBehaviours, here is the link to it: https://unity3d.com/learn/tutorials/modules/beginner/5-pre-order-beta/state-machine-behaviours

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 Vergil27 · May 27, 2016 at 07:58 AM 0
Share

$$anonymous$$d of "noOfClicks = $$anonymous$$athf.Clamp( click , 0 , 3);" what click mean ??, i don't create click before,

avatar image UsmanAbbasi Vergil27 · May 29, 2016 at 06:41 AM 0
Share

It is a typo error. It is not "click" it should be "noOfClicks".

avatar image allcoder · Apr 10, 2017 at 10:38 AM 0
Share

This is a greate solution, i did that and work awesome

https://www.youtube.com/watch?v=xfwty_31$$anonymous$$zc

avatar image youhavehowie · Apr 17, 2018 at 05:20 PM 0
Share

What are you doing for the state transitions though? I keep getting locked into one attack state because I'm trying to use the attack bools to transition.

avatar image vigeriegofelipe · Sep 29, 2018 at 07:45 PM 0
Share

it didnt work for me at first but then it worked puting this: if( noOfClicks >= 2 ) { animator.SetBool ("Attack2" , true); } in the update method in player script.

avatar image dolminowijaya11 · Mar 08, 2020 at 05:23 PM 0
Share

its didnt works for me, onstateexit never getting called, why ??

avatar image
0

Answer by PrettyLyn · Sep 06, 2018 at 01:54 PM

im experiencing this error..

Assets/Attack1.cs(23,8): error CS0103: The name `noOfClicks' does not exist in the current context

Comment
Add comment · Show 2 · 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 Korean4ever · Sep 07, 2018 at 07:51 AM 0
Share

Did you set 'noOfClicks' as an variable?

avatar image GoldenWol Korean4ever · Jul 25, 2021 at 09:40 PM 0
Share

after making it a veritable how can i get it from my player script

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

68 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

Related Questions

how can i make simple combo attack,, 0 Answers

Problem with combo attack -1 Answers

Problem with simple attack Attack ui Button 1 Answer

How to make more combo attack,, 1 Answer

any triple mouse click c# script? 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