# How to create a dynamic Map in Unity (Augmented Reality)

I created a dynamic map with a LineRenderer. The LineRenderer gets a new position assigned, for each movement my 'character' does. It works good, but I expect it to get a really bad performance at some point. Could you recommend a better solution to create a dynamic map with the free version of Unity. My code looks like this at the moment :

        if (lastPosition != target.transform.position) {
path.SetVertexCount(positionCounter + 1); //Path is the LineRenderer
lastPosition = target.transform.position;
path.SetPosition(positionCounter++,new Vector3(lastPosition.x,0,lastPosition.z));
}


The LineRenderer lies on a layer, which can only be seen by my orthographic mini map camera. Btw. I need it in a dynamic way, because it's an augmented reality app, which has no terrain which I could display on the map or stuff like this. I have to create the stuff which should be displayed.

• 1. You're just asking about the line denoting the player's path, right? 2. Can you provide a screenshot? 3. Are you just worried that a LineRenderer with a ton of vertices will get slow eventually? – Chris Mills-Price Mar 17 '16 at 0:56
• 1. Yes 2. I will add it 3. Yes I am worried about that the application will be slow when the amount of positions grows. – TobiasW Mar 17 '16 at 7:28

Caveats

1. I recommend anyone using LineRenderer to look into Vectrosity on the Unity asset store. I use it for any line drawing I do in any of my projects, period. It's great.

2. Before you implement a convoluted solution to address the performance impact of your current solution, prove that it's a problem with profiling. AR/VR is new, unfamiliar, and uncomfortable for most players. They are not likely to play any game for multiple hours yet (expect 15-30 minutes), so you don't need to consider that kind of long-term stability/performance.

If you're producing a brisk 1 point per second and you have a player stay for a massive 10 hours you're only at 36,000 points. Even if none of those are culled, that shouldn't cause extreme performance issues.

OK, so lets assume in spite of the above you're observing real performance issues that are definitely caused by the LineRenderer. Here are some ways to address that.

Cull Mercilessly

First, you need to make sure you're not rendering anything unnecessarily. I believe Unity will cull the parts of the line that aren't in the frustum, though I haven't explicitly tested that recently. If you zoom into the cube in Vectrosity's Demo #3, there appears to be culling, though whether that's Vectrosity, the demo, or Unity, I haven't checked just yet.

If it does not cull for you, then you may benefit from doing your own. There are many ways to accomplish this, but mostly they hinge around determining which points should be visible (perhaps using QuadTrees/Octrees) and re-creating your line periodically to show only those points. You don't need to maintain an object that can't possibly be visible.

Likely, this also involves breaking your line into multiple LineRenderers. I.e. if a player walks down one side of a street and then later the other side of the street, you shouldn't be trying to render everything between those two paths.

Compress Aggressively

A second approach is line "compression" - removing unnecessary points. Players generally won't care too much about having a perfect record of their path, so you can remove points from your line once you recognize they're not helping.

A good starting point for this is the Ramer-Douglas-Peucker algorithm.

Is It Really Necessary?

The easiest way to optimize any task is to realize you don't need to do most of that task. If you don't think the player really needs to know their long-term history, you could simply keep track of the past X points and only draw those.

I realize this is all pretty high-level. If you'd like more detail on any of these, or if I totally missed your actual question, let me know. :)

• Thanks for this very good answer, it helps me a lot. The high-level is sufficient for me thanks :) – TobiasW Mar 18 '16 at 7:17