Unity3D development (5): Unity3D 4.x use Mecanim to achieve combo

Source: Internet
Author: User

Unity3D 4.x and later versions provide a new Animation mechanism, Mecanim. Although the previous animations are also supported, the Sprite Animation in the Unity3D 4.3 preview version is also based on Animator, we can see that Mecanim will be the trend of playing animations in the future!

Mecanim is a structure based on a state machine. Different States represent an action (or multiple actions are merged, or a sub-state machine). A component association called a Transition is used between States, some conditions can be set in the Transition. when the conditions are met under the Transition "Source status", the system will automatically jump to the Transition "Destination Status "! (For specific usage, refer to the Unity3D manual Mecanim)

In some ARPG games, for example, DNF of client games and jieguan games, a set of combined attacks are triggered when you press the attack key continuously. In this process, the enemy is usually hard and straight, in addition, the final attack of a combination is generally a severe attack, which not only increases the refreshing feeling of a combo attack, but also promotes players to adopt certain attack strategies during the attack!

The State Machine Design of Mecanim makes it easy for developers to achieve this effect!


1. animation state machine

Here, we only use four actions to describe, including the standby status (Idle), attack 1 (AtkSlice), attack 2 (AtkStab), and attack 3 (AtkCleave ), the three attack statuses indicate the statuses triggered when the "common attack key" is continuously pressed, which are sequential,That is, if you press the "attack key" in the STANDBY state, attack 1 is entered; if you continue to press the "attack key" in the attack 1 State, attack 2 is entered, if you do not press the "attack key", the system will return to the standby status.The same is true for attack 2 to attack 3. Attack 3 is considered as a severe attack, that is, the connection is terminated and the attack returns to the STANDBY state!

The status chart is as follows:


I want to complete this function with the least code and setup, so I added only one state machine parameter ActionCMD, assuming ActionCMD = 1 is the Attack Status Parameter

(1) in any of the three attack states, if you do not continue to press the "attack key", the system will return to the STANDBY state, that is, 3 white Transition (2) on standby, attack 1, under attack 2, if you continue to press the "attack key", the system will jump to the "Attach" status (that is, the next attack status), that is, 3 blue Transition
2. Code implementation (1) After the state machine is set, you must complete the judgment on the current state and the setting of state parameters in the code to switch the animation state machine!

[Csharp]View plaincopyprint?
  1. // Use string variables to save the current status to avoid errors caused by multiple references
  2. Private static readonly string IdleState = "BaseLayer. Idle ";
  3. Private static readonly string AtkSliceState = "BaseLayer. AtkSlice ";
  4. Private static readonly string AtkStabState = "BaseLayer. AtkStab ";
  5. Private static readonly string AtkCleave = "BaseLayer. AtkCleave ";
  6. // Animation state machine parameter Key
  7. Private static readonly string ActionCMD = "ActionCMD ";
  8. Private Animator animator = null;
  9. // Current number of consecutive attacks (that is, the number of times the player presses the attack key)
  10. Private int curComboCount = 0;

(2) obtain the Animator component reference in Start ().

(3) In Update (), status switching is promoted based on the current status and input parameters.

[Csharp]View plaincopyprint?
  1. Void Update ()
  2. {
  3. AnimatorStateInfo stateInfo = this. animator. GetCurrentAnimatorStateInfo (0 );
  4. If (! StateInfo. IsName (IdleState ))
  5. {
  6. // After each parameter is set, the parameter settings should be cleared at the beginning of the next frame to avoid continuous switching.
  7. This. animator. SetInteger (ActionCMD, 0 );
  8. }
  9. If (stateInfo. IsName (AtkSliceState) & (stateInfo. normalizedTime> 0.6f) & (this. curComboCount = 2 ))
  10. {
  11. // When attack 1 is in the state and the current State runs for 0.6 orthogonal times (that is, 60% of the action duration), and the user presses the "attack key" in the state of attack 1"
  12. This. animator. SetInteger (ActionCMD, 1 );
  13. }
  14. If (stateInfo. IsName (AtkStabState) & (stateInfo. normalizedTime> 0.8f) & (this. curComboCount = 3 ))
  15. {
  16. // Block attack 2 (same as attack 1)
  17. This. animator. SetInteger (ActionCMD, 1 );
  18. }
  19. If (Input. GetKeyUp (KeyCode. J ))
  20. {
  21. // Listener user input (assuming the J key is the attack key)
  22. Attack ();
  23. }
  24. }

[Csharp]View plaincopyprint?
  1. Void Attack ()
  2. {
  3. AnimatorStateInfo stateInfo = this. animator. GetCurrentAnimatorStateInfo (0 );
  4. If (stateInfo. IsName (IdleState ))
  5. {
  6. // In the standby status, press the attack key to enter the attack 1 State and record the number of consecutive attacks as 1
  7. This. animator. SetInteger (ActionCMD, 1 );
  8. This. curComboCount = 1;
  9. }
  10. Else if (stateInfo. IsName (AtkSliceState ))
  11. {
  12. // In attack 1, press the attack key to record the number of consecutive attacks as 2 (switching status in Update)
  13. This. curComboCount = 2;
  14. }
  15. Else if (stateInfo. IsName (AtkStabState ))
  16. {
  17. // In attack 2, press the attack key and record that the number of consecutive hits is 3 (the switching status is in Update)
  18. This. curComboCount = 3;
  19. }
  20. }


Note that the values 0.6 and 0.8 used in Update () should be less than the Exit Time Parameter in which the current status is redirected to the standby status (this Time is also orthogonal)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.