In order to enrich our game, we often add the walking route to the characters (monsters) in the game, I want to use the Itweenpath plug-in implementation, but have not found the right way, because do not know how to achieve the implementation of the terrain height, Or if you use the Role controller move (Charactercontroller), how to use the Itweenpath driver? I am stupid, I realized a (here is just using Itweenpath to draw out the points), but also a point, if the reader knows how to implement a simpler way, please tell it! Progress together!
Let's take a look at the final:
There are two characters in the scene, then they will move randomly on the line drawn by Itweenpath! Let's set up a test scenario first, such as:
We then use Itweeneditor to edit the marching path of the characters in the scene, such as:
Behind, we need to implement the logic of walking, get the points on the Itweenpath curve, mentioned in the previous article, detailed can see this link, then we create a new RoleController.cs file, and then write our code, all the code is as follows:
Using unityengine;using System.collections;public class Rolecontroller:monobehaviour {public Itweenpath tweenPath;/// <summary>///the number of points above the curve, the more points move smoother//</summary>public int pointsize = 5;///<summary>///character move speed//< /summary>public float speed = 3f;public animationclip walkclip;public animationclip idleclip;private Vector3[] Pathpositionlist;private Vector3 pathpoint;private vector3[] positionlist;private Vector3 nextpoint;private Vector3 direction;private int moveindex;private bool movestatus;private bool idlestatus;private Animation animation;void Awake ( ) {this.pathpositionlist = Pointcontroller.pointlist (TweenPath.nodes.ToArray (), this.pointsize); this.animation = This. Getcomponent<animation> (); this.moveindex = 0;this.movestatus = False;this.idlestatus = False;if ( This.pathPositionList.Length > 0) {this.pathpoint = this.pathpositionlist [Random.range (0, This.pathPositionList.Length)];}} void Start () {this.transform.position = this. Getterrainposition (This.pathpoint); this. Startcoroutine (this. Setnextpositionlist (0));} void Update () {this. Setmovedirection (); Setmoveposition ();} <summary>///set Move vector///</summary>protected void Setmovedirection () {if (this.positionlist = = null) Return;if (This.moveindex < This.positionList.Length) {if (!this.movestatus) {this.pathpoint = this.positionlist[ This.moveindex];this.nextpoint = this. Getterrainposition (this.pathpoint); this.direction = (this.nextpoint-this.transform.position). Normalized * This.speed;if (this.direction! = Vector3.zero) {this.transform.rotation = Quaternion.lookrotation (New Vector3 ( This.direction.x, 0f, this.direction.z)); this.movestatus = true;} Else{this.moveindex + +;}}} Else{if (!idlestatus) {this.idlestatus = True;this.animation.crossfade (this.idleClip.name); Startcoroutine (this. Setnextpositionlist (5));}} <summary>///set Move position//</summary>protected void Setmoveposition () {if (this.positionlist = = null) return ; if (!this. Isarriveposition ()) {//This.characterController.Move (this.direction * time.deltatime); You can cancel this sentence, and note the following sentence, you can use the role controller to move this.transform.position = getterrainposition (this.transform.position + This.direction * time.deltatime);} else {this.transform.position = This.nextpoint;this.movestatus = False;this.moveindex + +;}} protected IEnumerator setnextpositionlist (int sceond) {if (Sceond > 0) {yield return new waitforseconds (5);} else {Yiel d return null;} int index = this. Getindexbylist (This.pathpositionlist, This.pathpoint); if (Index! =-1) {int nextindex = random.range (0, This.pathPositionList.Length); if (index! = nextindex) {int beginindex = index > nextindex? nextindex:index;int endinde x = index > Nextindex? Index:nextindex; vector3[] positionlist = new Vector3[endindex-beginindex];int positionlength = positionlist.length;if (Index > NextIn Dex) {for (int pathindex = endIndex, Positionindex = 0; Pathindex >= beginindex && Positionindex < Positionlen Gth Pathindex--, Positionindex + +) {POsitionlist[positionindex] = This.pathpositionlist[pathindex];}} else{for (int pathindex = beginindex, Positionindex = 0; Pathindex <= endIndex && Positionindex < Positionlen Gth Pathindex + +, Positionindex + +) {Positionlist[positionindex] = This.pathpositionlist[pathindex];}} This.moveindex = 0;this.movestatus = False;this.idlestatus = False;this.animation.crossfade (this.walkClip.name); This.positionlist = Positionlist;}}} <summary>///Get point location///</summary>///<returns>the terrion position.</returns>///< param name= "position" >position.</param>private Vector3 getterrainposition (Vector3 position) {Vector3 Terrainposition = new Vector3 (position.x, POSITION.Y, position.z); terrainposition.y = Terrain.activeTerrain.SampleHeight (terrainposition); return terrainposition;} <summary>///arrival at the specified location///</summary>///<returns><c>true</c> If this instance is Arrive position; Otherwise, <c>false</c>.</returns>private bool Isarriveposition () {Vector3 currentdirection = (This.nextpoint-(this.transform.position + This.direction * time.deltatime)). Normalized;if (this. Calculatenormalized (currentdirection) = = this. Calculatenormalized (this.direction) *-1) {return true;} return false;} <summary>///compute vector Standard///</summary>///<returns>the normalized.</returns>///<param Name = "Data" >data.</param>private Vector3 calculatenormalized (Vector3 data) {Vector3 position = Vector3.zero; position.x = data.x >= 0? 1: -1;position.z = data.z >= 0? 1: -1;return position;} private int getindexbylist (vector3[] positionlist, Vector3 position) {int index = 0;foreach (Vector3 item in positionlist) {if (item.x = = position.x && item.y = = position.y && item.z = position.z) return index;index + +;} return-1;}}
We then mount the RoleController.cs script to the characters in the scene and set the relevant properties, such as:
Finally run the game, you can see the character in the scene by the line to walk!
Original link: http://www.omuying.com/article/43.aspx
Unity3d for Monster Patrol, walk-by-line operation