[Unity3D] Unity3D game development-Summary of Three-hitting effects of ACT games and unity3d Game Development

Source: Internet
Author: User

[Unity3D] Unity3D game development-Summary of Three-hitting effects of ACT games and unity3d Game Development

Dear friends, I'm Qin Yuanpei. Welcome to follow my blog. My blog address is blog.csdn.net/qinyuanpei. After studying the redirection feature of the Unity3D Mecanim animation system, let's continue to explore more features of the Mecanim animation system. Today, the blogger wants to share with you the implementation of the three-hit effect in ACT games. As Unity3D currently has two types of Animation components: Animation and Animator, therefore, this article will explain the three-stroke effect implementation of these two types of Animation components. The Animation component is an Animation component used in Unity3.5 or earlier versions, the Animator component is currently used by the Unity3D Mecanim animation system.
First, let's take a look at the specific process of the effect of a three-Click Attack. Assuming that the role is currently in the Idle State, if the player presses the attack key, it enters the Attack1 state. If it is within the specified time, the player continues to press the attack key to enter the Attack2 state; otherwise, the player returns to the Idle state; similarly, if the role is in the Attack2 state, the player enters the Attack3 State if the player presses the attack key at this time; otherwise, the player returns the Idle state; after Attack3 ends, it returns to the Idle status, waiting for the player to trigger the next attack. From this, we can summarize the status changes of the Three-Click Attack:
(1) Idle-> Attack1-> Idle
(2) Idle-> Attack1-> Attack2-> Idle
(3) Idle-> Attack1-> Attack-> Attack3-> Idle
We can consider using two methods to achieve the effect of three-connection attacks through status changes. The first idea is that state animations are independent of each other, and the overall animation effect is achieved through State switching. The second approach is that the artist uses the State animation in sequence, and the programmer controls the animation effect based on time. Today, we mainly adopt the first method to better understand the finite state machine concept in game design and apply it to game development. Now, let's start today's content!


1. Animator Components

The Animator component is the animation component used by the Unity3D Mecanim animation system. This component uses the animation or Controller to control the animation. It is the main character of our project today. A female warrior with a left hand shield and a right hand sword.


First, create an Animator Controller and name it SwordGirlController. Double-click it to open the Animator window. Based on our discussion of status changes, we can easily design the following state model:


Here we define an integer variable ActionID. Its default value is 0. When the value of ActionID is 1, the role switches from Idle to Attack1. When the value of ActionID is 2, the role switches from Attack1 to Attack2. When the value of ActionID is 3, the role switches from Attack2 to Attack3. The switching condition for all connections pointing to the Idle is that the ActionID value is 0. In this way, an animation switching state model is created. Now let's write a script to control the animation:

// Triplicate Effect Based on the Mecanim animation system. The biggest problem currently is that the player cannot automatically restore to the Idle status after the attack, you need to perform an attack to return to the Idle state using UnityEngine; using System. collections; public class SwordGirlScript: MonoBehaviour {// Mecanim animation component private Animator mAnimator = null; // animation status information private AnimatorStateInfo mStateInfo; // defines the State constant value, do not include a layer name. Otherwise, the animation State private const string IdleState = "Idle" cannot be determined. private const string Attack1State = "Attack1"; private const string Attack2State = "Atta Ck2 "; private const string Attack3State =" Attack3 "; // defines the number of times a player repeatedly hits. private int mHitCount = 0; void Start () {// obtain the animation component mAnimator = GetComponent <Animator> (); // obtain the status information mStateInfo = mAnimator. getCurrentAnimatorStateInfo (0);} void Update () {// if the player is under attack and the attack has been completed, return to Idle status if (! MStateInfo. isName (IdleState) & mStateInfo. normalizedTime> 1.0F) {mAnimator. setInteger ("ActionID", 0); mHitCount = 0;} // if you press the left mouse button, the attack starts if (Input. getMouseButton (0) {Attack () ;}} void Attack () {// get status information mStateInfo = mAnimator. getCurrentAnimatorStateInfo (0); // if the player is in the Idle state and the number of attacks is 0, the player will attack according to attack method 1. Otherwise, the attacker will attack according to attack method 2, otherwise, attack 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 ;}}}

In this way, we can achieve the three-connection effect in ACT games. Let's take a look at the final effect!

During the actual test, the blogger finds that the role cannot automatically restore to the Idle status after an attack is executed. Unless the player continues to press the attack key, the blogger has not found a solution, if you know the specific reason, tell the blogger, haha.


Ii. Animation Components

We know that the Mecanim animation system uses a state machine to control the animation. The animation System Used in Unity3D before version 3.5 follows the understanding of the blogger, in fact, it is a stateless animation. We can only use the name of the animation fragment to decide whether to play an animation fragment or switch the animation within a certain period of time. Therefore, if we want to use the Animation component to achieve the effect of three-connection attacks, we must implement a state machine structure on the basis of this component. Based on the results of the discussion, we know that there are four statuses of Attack1, Attack2, Attack3, and Idle throughout the three-connection, so that we can define an animation state of the enumeration type ActionState. Next, we can switch the attack animation based on the status value to achieve the effect of three consecutive hits. Let's take a look at the script:

Using UnityEngine; using System. collections; public class AttackScripts: MonoBehaviour {// current attack Animation; AnimationClip currentClip; // Animation component; Animation mAnimation; // Animation state enumeration public enum ActionState {Attack1, Attack2, Attack3, none} // current animation status; private ActionState mState = ActionState. none; // void AttackTrigger () {if (Input. getMouseButton (0) {if (mState! = ActionState. Attack1 & mState! = ActionState. Attack2 & mState! = ActionState. Attack3) {mState = ActionState. Attack1;} else if (mState = ActionState. Attack1 & mState! = ActionState. Attack2 & mState! = ActionState. Attack3 & mAnimation [currentClip. name]. time> 1.0F) {mState = ActionState. Attack2;} else if (mState = ActionState. Attack2 & mState! = ActionState. Attack1 & mState! = ActionState. attack3 & mAnimation [currentClip. name]. time> 1.0F) {mState = ActionState. attack2 ;}}// attack routine 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;} // 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 the 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 ();}}}

The effect of the combo is not interrupted, that is, all the animations are played at one time. If we keep only the third case in the Mecanim animation system, the effect will be the same. Well, today's content is like this. Thank you for your attention to my blog and hope you will like it.







Unity3D game development instances, source code recommendation books, or download Source Code addresses

Unity 3D Game Development

Questions about unity3D Game Development

I have been familiar with virtools and unity, and I feel that the engine has its own advantages. As long as I master all of them, I personally feel that unity is in line with my style. Not very gorgeous, but all game functions can be implemented. There are a lot of resources available for download in the unity store, and it is also very convenient to make 2d games.
You can also export your own game to your mobile phone. If you are narcissistic, you will feel satisfied. You can combine art and programming very well. There are very few talents in this field. It is not difficult to learn about unity. As long as you complete several game production examples with video, you can achieve most of the functions. To be proficient, you must study it.
There are many cracked versions of unity on the Internet, and 3.0 is enough. The cracked version is still stable. Occasional problems.
I feel that about 5000 of my laptop can meet the requirements, and I need to use a desktop computer to make a precision model.

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.