Search Unity

Draw navigation line between player and goal

Discussion in 'Navigation' started by qqepijackxu, Jun 14, 2019.

  1. qqepijackxu

    qqepijackxu

    Joined:
    Jun 14, 2019
    Posts:
    2
    Hi. I'm more 3d-modeler than coder and i'm doing this scene where are few important places and I want to have some navigation lines that would show the path to player (prefer lines not going through the walls). I'm thinking something like Dead Spaces deck nav where laserline shows path to objective goal. I'm using linerenderer and navmesh. With this simple code:


    Code (CSharp):
    1.  
    2. public Transform goal;
    3.     void Start()
    4.     {
    5.      
    6.         UnityEngine.AI.NavMeshAgent navline = GetComponent<UnityEngine.AI.NavMeshAgent>();
    7.         navline.destination = goal.position;
    8.     }
    9.  
    10.  
    11. }
    12.  
    It shoots the line to target and stays wobbling there. But how to do line between player and goal?

    EDIT: something like this:

     
    Last edited: Jun 15, 2019
  2. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    312
    You don't need to put the NavmeshAgent on the same object as the Line Renderer, because that will move the line renderer object.
    What you want to do instead is calculate the path between the player and destination, and draw the path in the LineRenderer by taking the path's corners.
    Pseudocode:
    Code (CSharp):
    1. NavMeshPath path;
    2. NavMesh.CalculatePath(player, destination, path); //Saves the path in the path variable.
    3. Vector3[] corners = path.corners;
    4. lineRenderer.SetPositions(corners);
    5.  
     
  3. qqepijackxu

    qqepijackxu

    Joined:
    Jun 14, 2019
    Posts:
    2
    Thank you so much! I got it working.