http://blog.csdn.net/onerain88/article/details/12854817
The Unity3d 4.x version provides a new animation mechanism Mecanim, although the previous animation is also supported, but the animation of the sprite in Unity3d 4.3 preview is also based on animator, Mecanim will be the future trend of animation playback!
Mecanim is a state machine-based structure in which different States represent an action (or multiple action fusions, or a sub-state machine), and the state is associated with a component called transition, and transition can set some conditions when the transition " Source status, after satisfying its condition, will automatically jump to transition "destination state"! (Please refer to Unity3d Manual for specific usage Mecanim)
In some ARPG games, such as the end of the DNF and the class of arcade games, continuous press attack will trigger a set of combo attacks, in this process the enemy is generally hard straight state, and the final combination of attack is usually a heavy strike, so that both increase the refreshing feeling of the combo, It also urges the player to take certain attack tactics when attacking!
Mecanim's state machine design is very convenient to enable developers to achieve this effect!
1. Animation state Machine
Here only 4 action description, Standby State (Idle), Attack 1 State (Atkslice), Attack 2 State (Atkstab), Attack 3 State (Atkcleave), 3 attack states respectively to indicate the state that triggered by "normal attack key" in succession, there is a sequence of relations, that is, if the "Attack key" is pressed in the standby state, it enters the attack 1 state, and if the "attack key" continues to be pressed in the attack 1 state, the attack 2 state is entered, and if the "attack key" is not pressed, the attack 2 state is the same as the attack 3 state; That's the end of the combo, back on standby!
The status diagram is as follows
I want to be able to do this with minimal code and setup, so I only added a state machine parameter actioncmd, assuming actioncmd = 1 is the entry attack state parameter
(1) In any 3 attack states, no longer continue to press the "attack button", then back to standby state, that is, 3 white transition (2) on standby, attack 1, Attack 2, continue to press "attack", then jump to the combo state (i.e. the next attack state), That is, 3 blue Transition 2. Code implementation (1) After the state machine setup is complete, it is to do in the code to complete the current state of the judgment, as well as the state parameter settings, to complete the animation state machine switch!
Use a string variable to save the current state, avoid multiple references to write wrong private static readonly string idlestate = "Baselayer.idle"; private static readonly string atkslicestate = "Baselayer.atkslice"; private static readonly string atkstabstate = "Baselayer.atkstab"; private static readonly string atkcleave = "Baselayer.atkcleave"; Animation state machine parameter key private static readonly string actioncmd = "Actioncmd"; Private Animator Animator = null;//Current number of hits (that is, the number of times the player presses the attack key) private int curcombocount = 0;
(2) Get the animator component reference in Start ()
(3) Promote state switching in update () based on current state and input parameters
void Update () { Animatorstateinfo stateInfo = this.animator.GetCurrentAnimatorStateInfo (0); if (!stateinfo.isname (idlestate)) {//each time the parameter is set, the parameter setting should be emptied at the beginning of the next frame to avoid continuous switching This.animator.SetInteger ( Actioncmd, 0); } if (Stateinfo.isname (atkslicestate) && (Stateinfo.normalizedtime > 0.6f) && (This.curcombocount = = 2 ) {//When in Attack 1 state, and the current state runs 0.6 quadrature time (i.e. 60% of the duration of the action), and the user presses the "attack key" This.animator.SetInteger (Actioncmd, 1) in the attack 1 state ; } if (Stateinfo.isname (atkstabstate) && (Stateinfo.normalizedtime > 0.8f) && (This.curcombocount = = 3) {//block in Attack 2 state (same as Attack 1 state) This.animator.SetInteger (Actioncmd, 1); } if (Input.getkeyup (KEYCODE.J)) {//Listen for user input (assuming J key is attack key ) Attack ();} }
void Attack () { Animatorstateinfo stateInfo = this.animator.GetCurrentAnimatorStateInfo (0); if (Stateinfo.isname (idlestate)) {//In standby state, press the attack key, enter the attack 1 status, and record the number of hits as 1 this.animator.SetInteger (actioncmd, 1) ; This.curcombocount = 1; } else if (Stateinfo.isname (atkslicestate)) {//In Attack 1 state, press the attack key and record the number of hits as 2 (toggle State in Update ()) this.curcombocount = 2; } else if (Stateinfo.isname (atkstabstate)) {//In Attack 2 state, press the attack key and record the number of hits as 3 (toggle State in Update ()) This.curcombocount = 3; } }
It is important to note that the 0.6 and 0.8 used in update () should be less than the current state to jump to the standby status parameter exit time (which is also orthogonal)
Unity3d Game Development of Act game three-combo effect implementation overview
Http://wmyouxi.com/a/12582.html
After studying the redirection characteristics of the Unity3d Mecanim animation system, today we continue to explore more features of the Mecanim animation system. What bloggers want to share with you today is the implementation of the three-combo effect in the Act game, as Unity3d currently has the animation and animator two types of animation components, so this article will talk about the implementation of the three-combo effect of the two types of animation components, wherein the animation component Is the animation component used by the following versions of Unity3.5, the animator component is the animation component used by the Mecanim animation system currently unity3d.
We first to understand the three-combo effect of the specific process, assuming that the role is currently idle, if the player presses the attack key to enter the ATTACK1 state, if within the specified time, the player continues to press the attack key to enter the Attack2 state, otherwise return to the idle state; If the role is in the Attack2 state, if the player presses the attack key then enters the ATTACK3 state, otherwise returns to the idle state, when the ATTACK3 state is finished, it will return to the idle state, waiting for the player to trigger the next attack. Thus we can sum up the state change of three combos:
(1) Idle->attack1->idle
(2) Idle->attack1->attack2->idle
(3) Idle->attack1->attack->attack3->idle
We can consider using two kinds of ideas to realize the three-combo effect through the state change situation. The first idea is that each state animation is independent of each other, and the whole animation effect is realized by state switching. The second idea is that the artist takes each state animation in sequence, and the programmer controls the animation effect based on time. Today we mainly use the first method, the purpose is to grasp the game design of the finite state machine idea, it will be better applied to game development. OK, let's start today's content!
First, Animator component Chapter
The animator component is an animation component used by the Unity3d Mecanim animation system. This component uses the animator controller to control the animation. is the protagonist of our project today, a female warrior with a left hand holding a shield and a sword on his right hand.
Let's first create a animator controller and name it Swordgirlcontroller, double-click the Open Animator window, and with our discussion of state changes, we can easily design the following state model:
Here we define an integer variable ActionId with a default value of 0. When the value of ActionId is 1 o'clock, the role is switched from idle to Attack1, and when the value of ActionId is 2 o'clock, the role is switched from Attack1 to Attack2, when ActionId is toggled from Attack2 to ATTACK3 for 3 o'clock role. The switching condition of all points to the idle connection is the value of ActionId 0. So we set up a state model for animation switching. Well, let's write a script to implement the control of the animation:
[JavaScript] Plain Text view copy code
//基于Mecanim动画系统的三连击效果,目前最大的问题就是玩家在攻击后无法
Unity3d 4.x for combo using Mecanim