Note: There is a penetration problem with the line drawn with debug
No good explanation, just look at the code:
usingUnityengine;usingSystem.Collections;usingSystem.Collections.Generic;/** Can not find the method to set the line width, the current solution is to use the picture rectangle instead of the line to achieve the thickness of the line*//// <summary>///This script must be placed on the camera to see the painting content, Debugdraw can not, but the content of Debugdraw painting///can only be seen in edit mode. /// </summary>[Requirecomponent (typeof(Camera))] Public classdrawtrack:monobehaviour{/// <summary> ///the material of the brush/// </summary> PublicMaterial Material; /// <summary> ///painting The moving path of the target/// </summary> PublicTransform Target; /// <summary> ///Brush Smoothing Degree/// </summary> Public floatSmooth =1; /// <summary> ///Do you also draw a line in the editor ?/// </summary> Public BOOLDebugdraw =true; /// <summary> ///the collection of points that hold the move path/// </summary> PrivateList<vector3>path; /// <summary> ///the last moving point of the target/// </summary> PrivateVector3 lastposition; voidStart () {if(Material = =NULL) {Debug.logerror ("Please first give the script Material !!"); } if(Target = =NULL) {Debug.logerror ("Please set the target"); } Path=NewList<vector3>(); Lastposition=target.position; } voidUpdate () {if(Vector3.distance (Target.position, lastposition) >Smooth) {path. ADD (target.position); Lastposition=target.position; } } /// <summary> ///The GL drawing must be done in this function/// </summary> voidOnpostrender () {GL. Pushmatrix (); Material.setpass (0); //to draw a 2D segment, uncomment Gl.loadortho (); //GL. Loadortho ();GL. Begin (GL. LINES); /******* here to paint *********/drawlines (path. ToArray ()); Gl. End (); Gl. Popmatrix (); } Private voidDrawLine (Vector3 start, Vector3 end) {GL. Vertex3 (Start.x, Start.y, start.z); Gl. Vertex3 (End.X, End.y, end.z); if(Debugdraw) {debug.drawline (Start, End, Color.Red,1); } } Private voidDrawLines (vector3[] points) {if(Points. Length = =0) { return; } for(inti =0; I < points. Length-1; ++i) {varStart =Points[i]; varEnd = Points[i +1]; Gl. Vertex3 (Start.x, Start.y, start.z); Gl. Vertex3 (End.X, End.y, end.z); if(Debugdraw) {debug.drawline (Start, End, Color.Red,1); } } } Public voidclearline () {path. Clear (); } }
Use GL to draw the character's moving path