[Unity3d] Unity3d Game Development of Act game three-combo effect implementation overview

Source: Internet
Author: User

Dear friends, Hello everyone, I am Qin Yuanpei, welcome to pay attention to my blog, my blog address is Blog.csdn.net/qinyuanpei. After studying the redirection characteristics of the Unity3d Mecanim animation system, today we continue to explore many other 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, because Unity3d now has the animation and animator two types of animation components, so this article will be about the two types of animation components of the three-combo effect of the implementation, The animation component is an animated component that is used by Unity3.5 the following version numbers, and the animator component is the animation component used by the Mecanim animation system at the moment Unity3d.
We first understand the three-combo effect of the detailed process, assuming that the role is currently idle, the player assumes that the attack key is entered into the ATTACK1 state, assuming that within the specified time, the player continues to press the attack key to enter the Attack2 state, otherwise return to the idle state; Assuming the role is in the Attack2 state, assuming that the player presses the attack key to enter the ATTACK3 state, otherwise it returns to the idle state, and when the Attack3 state ends, it returns 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 overall animation effect is achieved by state switching. Another idea is that the artist will animate each state in sequence, and the programmer can control the animation effect based on time. Today we mainly adopt 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. Well, here's what we're going to start today!


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 start by creating a Animator Controller and name it Swordgirlcontroller , double-click Open Animator form, according to our discussion of state changes, we are able to design the following state models very easily:


Here we define an integer variableActionId, whose default value is0. WhenActionIdthe value is1the role isIdleSwitch toAttack1, whenActionIdthe value is2the role isAttack1Switch toAttack2, whenActionIdto be3the role isAttack2Switch toAttack3. All the pointsIdlethe switch condition of the connection isActionIdthe value is0. So we set up a state model for animation switching. Well, let's write the script to implement the control of the animation:

Based on the three-combo effect of the Mecanim animation system, the biggest problem now is that the player cannot//voluntarily return to the idle state after the attack, need to run an attack ability to return to the idle state using unityengine;using System.collections;public class Swordgirlscript:monobehaviour {//mecanim animation component private Animator manimator=null;// Animation state information Private Animatorstateinfo mstateinfo;//define the state constant value, do not precede the layer name, otherwise cannot infer the animation state Private Const string idlestate= "Idle"; Private Const string Attack1state= "ATTACK1";p rivate const string attack2state= "Attack2";p rivate const string Attack3state= "ATTACK3";//define player hit times private int mhitcount=0;void Start () {//Get animation component Manimator=getcomponent<animator > ();//Get status information mstateinfo=manimator.getcurrentanimatorstateinfo (0);} void Update () {//If the player is in an attack state and the attack is complete, return to the idle state if (!mstateinfo.isname (idlestate) &&        mstateinfo.normalizedtime>1.0f) {Manimator.setinteger ("ActionId", 0); mhitcount=0;} If you press the left mouse button, start attacking if (Input.getmousebutton (0)) {Attack ()}}   void Attack () {//Get status information mstateinfo=manimator.getcurrentanimatorstateinfo (0); If the player is idle and the number of attacks is 0, then the player according to attack moves 1 attack, otherwise according to attack moves 2 attack, otherwise according to attack moves 3 attackClick if (Mstateinfo.isname (idlestate) && mhitcount==0 && mstateinfo.normalizedtime>0.50f) {manimator.  Setinteger ("ActionId", 1);   Mhitcount=1;  }else if (Mstateinfo.isname (attack1state) && mhitcount==1 && mstateinfo.normalizedtime>0.65f) {  Manimator.setinteger ("ActionId", 2);   mhitcount=2;  }else if (Mstateinfo.isname (attack2state) && mhitcount==2 && mstateinfo.normalizedtime>0.70f) {      Manimator.setinteger ("ActionId", 3); mhitcount=3;}}}

So that we can achieve the three-combo effect in the ACT game, let's take a look at the effect of the last!


Bloggers in the actual test process found that the role after running an attack can not voluntarily return to the idle state, unless the player continues to press the attack button, the blogger does not find a solution at the moment, assuming that friends know the detailed reasons, Be sure to tell the blogger, haha.


second, animation component Chapter

We knowMecanimThe animation system is controlled by the state machine, and the3.5version number used to beUnity3dThe use of the animation system, in accordance with the blogger's understanding, in fact, is a kind of non-state animation, we can only use the name of the animation paragraph to decide to play a certain animation segment or in a certain period of time to animate the switch. So, let's say we want to useAnimationcomponent to achieve a three-combo effect, it is necessary to implement a state machine structure on the basis of this component. Based on the results of the discussion, we know that there are four states throughout the triple combo processAttack1,Attack2,Attack3,Idle, so we can define an enumeration type of animation stateactionstate. Next we can switch the attack animation based on this state value, achieve the effect of three combos, we look at the script together:

Using unityengine;using System.Collections;    public class Attackscripts:monobehaviour {//current attack animation;    Animationclip Currentclip;    Animation components;    Animation manimation;    Animation State Enumeration Public enum Actionstate {Attack1, Attack2, Attack3, None}//Current animation state;         Private Actionstate mstate = Actionstate.none; The attack triggers void Attacktrigger () {if (Input.getmousebutton (0)) {if (mstate! = Actionstate.attack1 &&am P            Mstate! = Actionstate.attack2 && mstate! = actionstate.attack3) {mstate = Actionstate.attack1; }else if (mstate = = Actionstate.attack1 && mstate! = Actionstate.attack2 && mstate! = Actionsta Te. Attack3 && manimation[currentclip.name].time > 1.0F) {mstate = Actionstate.attac            K2;                    }else if (mstate = = Actionstate.attack2 && mstate! = Actionstate.attack1 && mstate! = Actionstate.attack3 && manimation[currentclip.name].time > 1.0F) {mstate = Actionstate.attack2;        }}}//attack routines void Attacks () {float delaytime =0.0f;                Switch (mstate) {case ActionState.Attack1:delayTime = -0.1f;                Manimation.crossfade ("Attack1", 0.15F);                Currentclip = manimation["Attack1"].clip;                             Break                Case ActionState.Attack2:delayTime = -0.1f;                Manimation.crossfade ("Attack2", 0.15F);                Currentclip = manimation["Attack2"].clip;            Break                Case ActionState.Attack3:delayTime = -0.1f;                Manimation.crossfade ("Attack3", 0.15F);                Currentclip = manimation["Attack3"].clip;                             Break        Case ActionState.None:break; }//If the attack animation is finished, switch to the idle state if (manimation[currentclip. Name].time > (manimation[currentclip.name].length +delaytime)) {mstate = Actionstate.none;        Currentclip = manimation["Idle"].clip;        }} void Awake () {//Get animation component;    Manimation = getcomponent<animation> ();        } void Start () {if (manimation.clip) {currentclip = Manimation.clip;        } else {currentclip = manimation["Idle"].clip;            }} void Update () {if (currentclip! = null) {Attacktrigger ();        Attacks (); }    }}

Here the combo effect is not set to interrupt, that is, all the animation once played. If we only keep the third case in the mecanim Animation system, then it achieves the same effect, which I hope you will notice. Well, today's content is this, thank you for your attention to my blog, I hope you can enjoy.


Daily Motto: No matter who we are in the present, we must try to be a lovely person. Do not blame who, do not laugh at who, do not envy who, do their own dreams, go their own way.


Like my blog Please remember my name: Qin Yuanpei, my blog address is Blog.csdn.net/qinyuanpei.
Reprint please indicate the source, this article Qin Yuanpei, this article source: http://blog.csdn.net/qinyuanpei/article/details/38023199


[Unity3d] Unity3d Game Development of Act game three-combo effect implementation overview

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.