Use GL to draw the character's moving path

Source: Internet
Author: User

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

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.