[Unity3d] The combination of self-seeking road and Mecanim animation system in Unity3d game development

Source: Internet
Author: User

Hello everyone, welcome to my blog, I am Qin Yuanpei, my blog address is Blog.csdn.net/qinyuanpei.

During this time, most of the effort was put on the official Demo sample project, mainly to hope to be able to dig out valuable things to share with you. So Bo and everyone can learn together. Well, then today bloggers want to share with you is the initiative to find the road and Mecanim animation system to achieve a small case, I hope to learn Unity3d can help you.

Bloggers told everyone before. Bo Master is a Paladin, usually learning programming tired, Bo Master will play the Paladin. Bloggers occasionally go back to play "new Paladin ol." The online game of the Paladin seems to have never been flat, from the original "Paladin ol" to today's "new Paladin Ol", the online game of the Paladin has been in a tepid state.


Although bloggers are more disgusted with online games. But as a blogger liked by the single-machine game adaptation of the online games, Bo Master still can't help to try a bit. Based on the Unity3d "new Paladin ol" although from the screen quality and similar web game opened up the distance, but in the game play and experience still do not get rid of the fixed mode of domestic online games, Bo Master sigh. This article is based on the design of the game on its own initiative to find the road. Speaking of their own initiative to find the way. This is almost the standard configuration for all the Web games now. In the blogger's opinion. Take the initiative to find a way to simplify the way players look for goals at the same time. Weakened the player's participation, so that the player should explore the world of the game themselves. into a dull mouse game. Once upon a distance, we explored the road in the maze, once, we opened the organ, cracked Zhenfa harvest the fun of the game. In such a fast-paced era, we can only recall the past life over and over. There were no computers, no smartphones, no movies ... But we often miss that time simple life, so, as a child, happiness is a simple matter, grow up, simple is a happy thing. Although bloggers hate to take the initiative to find the way to this setting. But this has nothing to do with our learning Unity3d. Because we are only to better use this engine to make good game products. Well, gossip less, we continue to say, "New Paladin ol" in their own initiative to find the way in the game, the player can control the characters by the mouse, assuming a far distance. The character will reach the target in the form of a run, otherwise the character will walk to the target position. And that's what we want to achieve today. All right. The following we formally start today's content, today's content, divided into two parts, the first part of the Mecanim animation system, the second part of the initiative to find the road.

The first part: Mecanim animation system

In this part, we mainly talk about the switch of the animation, because from the descriptive narrative we know that the role of animation has three states, namely, Idle, Walk, Run.

Through the previous study. We know that the state machine of the Mecanim animation system makes it very easy to switch the animation state. So we can figure out a general idea, mouse click to get the position of the mouse, and then we use the method of ray, from the camera to launch a ray through that point, then the intersection of the Ray and the ground is the target point of our search, we calculate the role to use the distance between the target point to determine what kind of animation, And the pathfinding to the Unity3d nav Mesh agent components to complete, this is our implementation of the idea today, in fact, Bo in a previous article has already described this method, here is only on the basis of the previous. The combination of Mecanim animation system and its own active pathfinding components made some improvements. All right. We open our project, is a simple scene created by bloggers, the content of our today with this scenario to unfold:


Next. We use the Mecanim animation system to design character animations, and in today's project there are only three states, so we are able to connect three states, where we define two variables of type bool Iswalk and Isrun, which implicitly feels false, This is the two variable switch we used to switch animations today.

The rest of the work is to write scripts to control the animation, this step we put in the second part.



Part II: Take the initiative to find your way

In this part. We first bake the scene, because the nav mesh agent component calculates the path of the pathfinding based on the grid, so the baking process is equivalent to preserving the grid information in the scene. Only in this way do we have the ability to use Unity3d's pathfinding components.

Below we will explain the baking of the scene:

First we select objects in the scene that do not need to interact with the player, or we can feel that these objects are obstacles that our characters need to avoid, set them to static,


Next we open the navigation form with the Windowe->navigation command, where the selected object is shown in dark color. After confirming the error, click Bakebutton to bake the scene so that we are finished baking the scene.


All right. Below we select the roles in the scene. Add the Nav Mesh Agent component to it. The green area is displayed in the scene. The implication is that these regional roles can reach. About the parameters of the Nav Mesh agent component. We are able to consult the API documentation on their own, there is no explanation here, OK, here we write the script:

Using unityengine;using System.collections;public class Peoplescripts:monobehaviour {//Animation components private Animator manim;//    Move speed public float movespeed=2.5f;//pathfinding component private navmeshagent magent;//pathfinding target tag private gameobject ball;  Pathfinding Mark Prefab public gameobject prefabball;void Start () {//Get Animation component manim=getcomponent<animator> (); Get Pathfinding component Magent=getcomponent<navmeshagent> ();} void Update () {//press the left mouse button if (Input.getmousebutton (0)) {//Get mouse position Vector3 mpos=input.mouseposition;//use Ray method to get target position Ray mray= Camera.main.ScreenPointToRay (MPos); Raycasthit mhit;if (Physics.raycast (mray,out mhit)) {//here corresponds to the ground in the scene, walls, stairs three kinds of structure if (mhit.collider.tag== "Ground" | | mhit.collider.tag== "Wall" | |   mhit.collider.tag== "Ti") {//Get the target position Vector3 Mtarget=mhit.point; Use a fully target-oriented rotation transform.   LookAt (Mtarget);   Use smooth turn to turn to target//smoothrotate (mtarget);   Calculated distance float mdistance=vector3.distance (mtarget,this.transform.position); When the distance is greater than 4 o'clock run to the target position.  Otherwise walk to the target location if (mdistance>4f) {Manim.setbool ("Isrun", true); }else{Manim.setbool ("Iswalk", true);  }//To generate different heights of pathfinding targets according to different structures if (mhit.collider.tag== "Ground") {//Tag pathfinding target ball= (gameobject) Instantiate (prefabball,new  Vector3 (MTARGET.X,0.5F,MTARGET.Z), quaternion.identity); }else{//marker pathfinding Target ball= (gameobject) Instantiate (Prefabball,new Vector3 (mtarget.x,3.0f,mtarget.z), quaternion.identity  ); }//Set pathfinding target magent.setdestination (mtarget);}}} void Ontriggerenter (Collider mcollider) {if (mcollider.tag== "ball") {//Get target tag gameobject mball=mcollider.gameobject;/ /Destroy Target tag destroy (Mball);//Set role status to Idlemanim.setbool ("Isrun", false); Manim.setbool ("Iswalk", False);}} Smooth turn, reference from Stealthvoid smoothrotate (Vector3 target) {//Construction target toward quaternion targetrotation = Quaternion.lookrotation ( Target, vector3.up);//interpolation quaternion mrotation = Quaternion.lerp (transform.rotation, targetrotation, 15F * Time.deltatime);//assign Value transform.rotation=mrotation;}}

Finally, let's take a look at the final effect. In order to let everyone more clearly see the effect of road search, bloggers in the road to find a spot to add a purple ball, easy to observe:



Daily motto: don't bow, don't complain. They think you are invincible, then they like it, and others do not know, you do not have to refute to him to listen to.


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


[Unity3d] The combination of self-seeking road and Mecanim animation system in Unity3d game development

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.