PathDefinition.cs
1 usingUnityengine;2 usingSystem.Collections;3 usingSystem.Collections.Generic;4 usingSystem.Linq;5 6 Public classPathdefinition:monobehaviour7 {8 Publictransform[] Linepoint;9list<string> test =Newlist<string>();Ten One PublicIenumerator<transform>Getpathenumeratror () { A if(Linepoint = =NULL|| Linepoint.length <1) - yield Break; - the intDirection =1; - intindex =0; - - while(true) { + yield returnLinepoint[index]; - + if(Linepoint.length = =1)Continue; A at if(Index <=0) Direction =1; - Else if(Index >= (Linepoint.length-1)) Direction =-1; - -index = index +direction; - } - } in - voidOndrawgizmos () to { + if(Linepoint = =NULL&& Linepoint.length <2)return; - the ///filter NULL * varPoints = linepoint.where (t = = t! =NULL). ToList (); $ Panax Notoginseng if(Points.count <2)return; - the for(inti =1; i < linepoint.length; i++) + { AVector3 start = linepoint[i-1].position; theVector3 end =linepoint[i].position; + gizmos.drawline (start, end); - } $ $ } -}
FollowPath.cs
usingUnityengine;usingSystem.Collections;usingSystem.Collections.Generic; Public classFollowpath:monobehaviour { Public enumFollowtype {movetoward, lerp} PublicFollowtype Type =Followtype.movetoward; Publicpathdefinition Path; Public floatSpeed =1; Public floatMaxdistancetogoal =. 1f; IEnumerator<Transform>_currentpoint; Public voidStart () {if(Path = =NULL) { return; } _currentpoint=Path.getpathenumeratror (); _currentpoint.movenext (); //set position at the start point if(_currentpoint.current = =NULL)return; Transform.position=_currentpoint.current.position; } voidUpdate () {if(_currentpoint = =NULL|| _currentpoint.current = =NULL)return; if(Type = =followtype.movetoward) transform.position= Vector3.movetowards (transform.position,_currentpoint.current.position,speed*time.deltatime); Else if(Type = =followtype.lerp) transform.position= Vector3.lerp (transform.position, _currentpoint.current.position, Speed *time.deltatime); vardistancesquared = (Transform.position-_currentpoint.current.position). Sqrmagnitude; if(Distancesquared < Maxdistancetogoal *maxdistancetogoal) {_currentpoint.movenext (); } } }
The code is simple. I'm not commenting, it's better for path edit tracking
Follow Path-"Unity3d Generic Script