[Unity3D] combination of Unity3D game development and Mecanim animation system, unity3dmecanim

Source: Internet
Author: User

[Unity3D] combination of Unity3D game development and Mecanim animation system, unity3dmecanim

Hello, everyone. Welcome to my blog. I am Qin Yuanpei and my blog address is blog.csdn.net/qinyuanpei. During this time, the blogger spent most of his energy on the official example project, hoping to find valuable things and share them with you so that the bloggers can learn together. Well, what the blogger wants to share with you today is a small case of Combining automatic pathfinding with the Mecanim animation system. I hope it will be helpful for you to learn about Unity3D.

The blogger once told everyone that the blogger is a fan of xianjian. When he is tired of programming, the blogger will play xianjian. It is the new xianjian ol that the blogger occasionally goes back to play. xianjian's online games seem to have never been flat. From the initial "xianjian ol" to the current "New xianjian ol", xianjian's online games have been in a very bad state.


Although bloggers are disgusted with online games, as an online game adapted from a standalone game that bloggers like, the bloggers can't help but try it. Although the Unity3D-based New xianjian ol draws a distance from similar web games in terms of image quality, it still does not get rid of the fixed mode of Chinese online games in terms of gameplay and experience, the blogger sighed and wrote this article based on the game's automatic pathfinding design. When it comes to automatic pathfinding, this is almost the standard configuration of all current web games. In the opinion of bloggers, automatic pathfinding simplifies the way players search for their targets and weakens players' participation, the game world that should have been explored by players on their own has become a boring mouse game. Once upon a time, we explored the path far away in the maze, and once upon a time, we started authorities and cracked the formations to reap the pleasure of the game. In this fast-paced era, we can only review our past life over and over again. At that time, there were no computers, no smartphones, no movies ..... however, we often miss simple life at that time. Therefore, when we were young, happiness was a simple thing. When we grow up, simplicity was a happy thing. Although bloggers hate automatic pathfinding, this is irrelevant to learning Unity3D, because we just want to better use this engine to make good game products. Okay, let's talk less about it. Let's continue with the automatic pathfinding in "New xianjian ol". In the game, players can control the characters with the mouse. If the distance is far away, the character will arrive at the target location in the form of running, otherwise the character will walk to the target location. This is what we want to achieve today. Now let's start today's content. Today's content is divided into two parts: the first part is about the Mecanim animation system, and the second part is about automatic pathfinding.

 

Part 1: Mecanim animation System

In this section, we mainly talk about animation switching, because we know from the description above that there are three types of role animation states: Idle, Walk, and Run. Through the previous study, we know that through the Mecanim animation system's state machine, we can easily switch the animation state. Therefore, we can come up with a general idea to get the mouse position by clicking the mouse, and then use the ray method to emit a Ray from the camera that passes through the point, then, the intersection of rays and the ground is the target point of our pathfinding. We calculate the distance between the role and the target point to determine what animation the role should adopt, the path searching is done by the Unity3D Nav Mesh Agent component. This is our implementation idea today. The real blogger has already described this method in a previous article, this is just based on the previous improvements, combined with the Mecanim animation System and the automatic path finding component. Now, we open our project, which is a simple scenario created by the blogger. Our content today is based on this scenario:


Next, we use the Mecanim animation system to design the role animation. In today's project, the role has only three States, so we can connect the three States, here we define two Bool variables IsWalk and IsRun. The default value is False. This is the two variable switches we use to switch the animation today. The rest of the work is to write a script to control the animation. This step is for the second part.



Part 2: Automatic route searching

In this part, we first need to bake the scene, because the Nav Mesh Agent component calculates the route Finding Based on the grid, so the baking process is equivalent to saving the grid information in the scenario, only in this way can we use the Unity3D path finding component. Next we will explain the baking of the scenario:

First, we select objects that do not need to interact with players in the selected scenario, or we can think of these objects as obstacles that our roles need to avoid and set them to Static,


Next, open the Navigation window using the Navigation we-> Navigation command. The selected object is displayed in a dark color. After confirming the correctness, click the Bake button to Bake the scene, in this way, the baking work of the scenario is completed.


Now let's select the role in the scenario and add the Nav Mesh Agent component to it. The green area is displayed in the scenario, meaning that these regional roles can arrive. For the parameters of the Nav Mesh Agent component, you can refer to the API documentation on your own. I will not explain it here. Well, let's write the script below:

Using UnityEngine; using System. collections; public class PeopleScripts: MonoBehaviour {// animation component private Animator mAnim; // mobile speed public float MoveSpeed = 2.5F; // Pathfinder component private NavMeshAgent mAgent; // specify private GameObject Ball as the path finding target; // specify public GameObject PrefabBall as the path finding target; void Start () {// obtain the animation component mAnim = GetComponent <Animator> (); // obtain the path finding component mAgent = GetComponent <NavMeshAgent> ();} void Update () {// press the left mouse button if (Input. getMouseButton (0) {// get the mouse position Vector3 mPos = Input. mousePosition; // obtain the target position by using the Ray method. Ray mRay = Camera. main. screenPointToRay (mPos); RaycastHit mHit; if (Physics. raycast (mRay, out mHit) {// The structure corresponds to the ground, wall, and stair in the scenario. if (mHit. collider. tag = "Ground" | mHit. collider. tag = "Wall" | mHit. collider. tag = "Ti") {// obtain the target location Vector3 mTarget = mHit. point; // use a fully target-oriented rotating transform. lookAt (mTarget); // use a smooth turn to the target // SmoothRotate (mTarget); // calculate the float mDistance = Vector3.Distance (mTarget, this. transform. position); // when the distance is greater than 4, it will run to the target location. Otherwise, it will walk to the target location if (mDistance> 4F) {mAnim. setBool ("IsRun", true);} else {mAnim. setBool ("IsWalk", true);} // generate path finding targets with different heights based on different structures. if (mHit. collider. tag = "Ground") {// tag the target Ball = (GameObject) Instantiate (PrefabBall, new Vector3 (mTarget. x, 0.5F, mTarget. z), Quaternion. identity);} else {// mark the target Ball = (GameObject) Instantiate (PrefabBall, new Vector3 (mTarget. x, 3.0F, mTarget. z), Quaternion. identity);} // set the target mAgent. setDestination (mTarget) ;}}} void OnTriggerEnter (Collider mCollider) {if (mCollider. tag = "Ball") {// get the target tag GameObject mBall = mCollider. gameObject; // Destroy the target tag Destroy (mBall); // set the role status to IdlemAnim. setBool ("IsRun", false); mAnim. setBool ("IsWalk", false) ;}// smooth turn, refer to self-Stealthvoid SmoothRotate (Vector3 target) {// construct the target towards Quaternion targetRotation = Quaternion. lookRotation (target, Vector3.up); // interpolation of the target Orientation Quaternion mRotation = Quaternion. lerp (transform. rotation, targetRotation, 15F * Time. deltaTime); // assign a value to transform. rotation = mRotation ;}}

Finally, let's take a look at the final effect. To make it clearer to everyone, the blogger adds a purple ball at the pathfinding point for observation:



Daily Rumor: Don't bow your head, don't complain. They think you are invincible, so they like it. If others don't know, you don't have to refute it.


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/38981669



Unity3d File Import format

1. FBX,. dae (Collada),. 3DS,. dxf and. obj, preferably fbx
2. Yes, preferably T-Pose, and the skeleton name complies with mecanim specifications. In this way, mecanim can be automatically mapped without manual ing;
3. If it is a real scene, you can use camera animation. If it is a video, you can use mov format;
4 and 2 are acceptable, but most domestic max Users suggest max

Unity3d: Click the map to automatically find the path.

In this case, you need to create two rays, one of which is the one you wrote and the other is the camera of a small map. For example, when a small map camera is CamraTab, It is CamraTab. screenPointToRay (Input. mousePosition); actually the same as the main camera
 

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.