[Unity3D] Trigger-based animation design for Unity3D Game Development

Source: Internet
Author: User

Hello everyone, I'm Qin Yuanpei. Welcome to follow my blog. My blog address is blog.csdn.net/qinyuanpei.

I believe that the first thing you do after Unity3D is to browse the official example project Angry robot. this game has a good performance in scene rendering and light shadow effects. You may not remember this scenario. When a player controls our game role, robot, the door is automatically opened when the robot approaches the door in the game scenario, when the robot leaves the door, the door is automatically closed. So today we will implement such a simple function together. You can see through the question that the implementation of this function is based on the Trigger. Trigger is a Trigger in Unity3D. When the isTrigger option is selected for a collision generator, it becomes a Trigger. The difference between a trigger and a collision generator is that the collision generator responds through the OnCollisionEnter/Stay/Exit () method and will generate force on The objects involved in the collision. The trigger responds to the trigger through the OnTriggerEnter/Stay/Exit () method and does not act on the production force of the object involved in the trigger. About the trigger and collision, you can refer to this article: http://game.ceeger.com/Components/class-BoxCollider.html

Now, let's take a look at how to implement Trigger-based animation in Unity3D. First, we will create the following scenario:


In the scenario, the ground and the wall and the door are composed of cubes. Here we remove all the boxes of the ground and the wall to avoid interference with our programs. First, we will write two animations to control the movement of the door from left to right and from right to left:

// Open Door animation private void OpenDoorAni () {if (transform. position. x <= 1.8F) {transform. translate (Vector3.right * Time. deltaTime * 1.5F) ;}/// closed animation private void CloseDoorAni () {// Wait for 5 seconds before closing, to avoid the collision between the role and the door StartCoroutine ("Wait "); if (transform. position. x> = 0) {transform. translate (Vector3.left * Time. deltaTime * 1.5F );}}

The code is easy to understand, that is, to move the door from left to right and from right to left! Next, we will add a sphere collision tool to our role and check the isTrigger option. Why should we select the sphere collision tool? As a result, when the role is triggered and exited, they are all tangent to the box collision device of the door. When the door is closed, the delay is two seconds to give the player a reaction time to control the role and avoid collision with the door. Note that the role has a rigid body.

Now, let's write the code that triggers this part:

void OnTriggerEnter(Collider mCollider){  Debug.Log(mCollider.gameObject.tag);  if(mCollider.gameObject.tag=="Player")  {    Triggered=true;  }}void OnTriggerExit(Collider mCollider){  if(mCollider.gameObject.tag=="Player")  {    Triggered=false;  }}
Here, we change a bool-type flag variable to mark whether the role has triggered the trigger. When the trigger is triggered, we execute the Open Door animation, exit the animation to close the door when it is triggered. We put this part in the Update () method:

void Update () {  if(Triggered)  {    OpenDoorAni();  }else if(!Triggered)  {CloseDoorAni();  }}

Finally, all the code is provided:

Using UnityEngine; using System. collections; public class AniScripts: MonoBehaviour {// define the door's moving speed public float mSpeed = 1.5F; // define the door's flag variable private bool Triggered = false; void Update () {if (Triggered) {OpenDoorAni ();} else if (! Triggered) {CloseDoorAni () ;}// door opening animation private void OpenDoorAni () {if (transform. position. x <= 1.8F) {transform. translate (Vector3.right * Time. deltaTime * 1.5F) ;}/// closed animation private void CloseDoorAni () {// Wait for 5 seconds before closing, to avoid the collision between the role and the door StartCoroutine ("Wait "); if (transform. position. x> = 0) {transform. translate (Vector3.left * Time. deltaTime * 1.5F) ;}} void OnTriggerEnter (Collider mCollider) {Debug. log (mCollider. gameObject. tag); if (mCollider. gameObject. tag = "Player") {Triggered = true ;}} void OnTriggerExit (Collider mCollider) {if (mCollider. gameObject. tag = "Player") {Triggered = false ;}} IEnumerator Wait () {// Wait 5 seconds before executing yield return new WaitForSeconds (5 );}}

Let's take a look at the demo:


If you like my blog, please remember my name: Qin Yuanpei. My blog address is blog.csdn.net/qinyuanpei.
Reprinted please indicate the source, Author: Qin Yuanpei, the source of this article: http://blog.csdn.net/qinyuanpei/article/details/24198893


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.