[Unity3D] Unity3D game development-monster AI

Source: Internet
Author: User

Hello, everyone. Welcome to the Unity3D Game Development Series articles that I bring to you. My blog address is http://blog.csdn.net/qinyuanpei.

In the previous article, we basically implemented the function of a small map. Today, we are going to implement monster AI. The so-called monster AI means that we compile certain algorithms for monsters, it can be intelligent to a certain extent to enhance the playability of the game. In general RPG games, monsters usually patrol within the preset range of a game. When players enter the protection range of monsters, the monsters will change from patrol to attack, to attack players, let's implement a simple monster AI today. Let's take a look at the Code:

Using UnityEngine; using System. collee_; public class AI: MonoBehaviour {// defines four statuses of monsters: Standing, walking, running, idle public const int STATE_STAND = 0; public const int STATE_WALK = 1; public const int STATE_RUN = 2; // the current state of the monster private int NowState; // The Game role public GameObject Hero; // The time when the monster thinks public const int AI_THINK_TIME = 2; // The critical distance to trigger the monster attack is public const int AI_ATTACT_DISTANCE = 10; // last thought time private float LastThinkTime; void Start () {} void Update () {// if (Vector3.Distance (transform. position, Hero. transform. position) <AI_ATTACT_DISTANCE) {// The enemy starts to run this. getComponent <Animation> (). play ("run"); // The enemy enters the running state NowState = STATE_RUN; // enable the enemy to face the role transform. lookAt (Hero. transform); // approaching transform to the player. translate (Vector3.forward * Time. deltaTime * 5);} else {// when the difference between the current Time and the last thought Time is greater than the thought Time of the monster, the monster begins to think if (Time. time-LastThinkTime> AI_THINK_TIME) {// start to think about LastThinkTime = Time. time; // obtain the Random number int Rnd = Random between 0 and 3. range (); // assign different states and actions to the monster Based on the random number. switch (Rnd) {case 0: // standing status this. getComponent <Animation> (). play ("idle"); NowState = STATE_STAND; break; case 1: // walking status // rotate the monster to complete the walking action Quaternion mRotation = Quaternion. euler (0, Random. range (1, 5) * 90, 0); transform. rotation = Quaternion. slerp (transform. rotation, mRotation, Time. deltaTime * 1000); // play the animation this. getComponent <Animation> (). play ("walk"); // change the transform location. translate (Vector3.forward * Time. deltaTime * 3); NowState = STATE_WALK; break; case 2: // running status this. getComponent <Animation> (). play ("run"); transform. translate (Vector3.forward * Time. deltaTime * 5); NowState = STATE_RUN; break ;}}}}}

In the above Code, there are two conditions that can trigger the change of the monster status. First, the gamer enters the monster's alert range. At this time, the monster will be running close to the gamer. Second, the difference between the current time and the last time the monster thinks is greater than the time the monster thinks. At this time, the monster will respond randomly.

Okay. Let's go back to the game interface.


The blogger downloaded a character model from the official resource store in advance. In this model, the designer has designed a character animation for us. We drag and drop this model into the scene, adjust it to a proper position, and set the default animation of the character to idle, next we will drag and drop the script we just wrote to this model and set Hero as our Player Object. Here we use the third-person role Controller component officially provided, the final result is as follows:



We can see that at different times, monsters can perform different status actions on their own. When players are near monsters, they will be attacked by the enemy. This is what we are talking about today. Thank you!

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.