Show player path in Unity

Daniel Kirwan
4 min readAug 18, 2021

--

Objective: Using the navmesh show the path the player will take

Above you can see what will happen at the end of this article.

I went back to this project after playing the Ascent on Xbox game pass. You press a button and a line appears on the screen and shows the player where to go for their current quest. My line doesn’t quite look like it does in the Ascent but the principal is the same. Show the player a line of where they’re going or need to go to.

I will be using the Navmesh to help with knowing where the line is to appear.

I have added a Line Renderer component to the player, and added in a game object that will act as the marker. I added in an image that looks like a diamond.

I am using the diamond image as a material and below you can see the set up for the material.

I made it transparent so that the outer edges are not on show. If you’re going to follow along, I have the map marker object as a child of the player and it is turned off when the game starts. It will only be on when the player has clicked to move.

I have added the three variables above. The markerPrefab will be the marker that is on the player and the visual holder will be an empty game object in the hierarchy that will hold the marker when the player clicks. Once the player has reached their destination the marker will then be parented back to the player.

I created two new methods for this and I have adjusted another for it to work.

If the player has a destination to move towards I call ActivateMarker passing in the click position. I set the marker object to active, set the position to the Vector3 passed in and then set the parent to the visual marker.

In my destination reached method I am checking whether the distance is less than the threshold of 1. If it is, then the player stops, I added in this extra code to call another method to show the path.

If the player has reached its destination then the marker needs to be turned off and the parent reset to the player and to disable the line renderer.

In the start method I am setting up the line renderer.

The DrawPath method does what it says on the tin. It draws the path.

I am setting the lineRenderer position count to that of the number of corners that are in the path. You get this number from the object that has the navmesh agent component. I am then setting the initial position of the line to that of the player.

If there are no corners left which means that there is only 1 point left, then it will draw a straight line.

I am then using a for loop to go through each corner and then creating a new vector to show the next part of the path.

Now, back into Unity, don’t forget to add your new objects into their respective places.

If you play your game now you should see a line appear, it will most likely be purple. I created a small shader in URP and added it to the lineRenderer component as a material. That is how I got my orange line.

To create a material from a shader graph, you need to right-click on the shader graph in the project window and then select material. Then drag in the material to the material slot on the lineRenderer.

--

--

Daniel Kirwan
Daniel Kirwan

No responses yet