0
\$\begingroup\$

I have a set of pre-determined destinations. When the user chooses any one of those destinations, the path to the destination should be drawn, similiar to google maps. I am trying to do this in unity, but not able to figure out just how.

\$\endgroup\$
  • \$\begingroup\$ Are you asking for the determination of the points in path also? Or just for the drawing part? I mean, have you managed to calculation of the traversal points for reaching to those destinations? \$\endgroup\$ – Tolga Şen Aug 31 '17 at 12:49
  • \$\begingroup\$ I have used NavMesh.CalculatePath to determine the points in the path. Now, the drawing part is what I'm having trouble with. \$\endgroup\$ – azemda Aug 31 '17 at 13:07
0
\$\begingroup\$

You can use LineRenderer component that Unity provides. Attach it to the the appropriate GameObject, set the parameters of it in order to visualise the line however you desire and use the following code:

private Vector3[] wayPoints; //You have to set these waypoints before you start to use LineRenderer.
private LineRenderer lineRenderer;

void Start()
{
    lineRenderer = GetComponent<LineRenderer>();
    lineRenderer.positionCount = wayPoints.Length;
    for (int i = 0; i < wayPoints.Length; i++)
    {
        lineRenderer.SetPosition(i, wayPoints[i]);
    }
} 

You might want to check the manual out as well.

\$\endgroup\$
  • \$\begingroup\$ positionCount is the number of line segments, so positionCount is the total number of line segments, where each line segment has two waypoints as endpoints, right? So 'lengthOfLineRenderer' is 'wayPoints.Length - 1' right? \$\endgroup\$ – azemda Aug 31 '17 at 13:33
  • \$\begingroup\$ Oh my mistake, I have edited that part. PositionCount is basically the number of points that lineRenderer uses to draw the lines. Say there are 10 points, there will be 9 vertexes in the line for instance. \$\endgroup\$ – Tolga Şen Aug 31 '17 at 13:54
  • \$\begingroup\$ So, positionCount is waypoints.Length, and not waypoints.Length - 1, right? \$\endgroup\$ – azemda Aug 31 '17 at 14:03
  • \$\begingroup\$ Yes, that's right. And the important thing is, wayPoints must include the position that player stands at the beginning. \$\endgroup\$ – Tolga Şen Aug 31 '17 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.