# How can I make a trail renderer flat with the normal of the nearest face?

I'm making a basic skid system for a car model using a trail renderer. I have everything setup and working properly except that the trail renderer always faces the camera. I want the trails to be perfectly flat with the ground. So I thought if unity can make the faces all rotate towards the camera, then would it be able to rotate towards the normals of the nearest faces?

Well, the thing is, trail renderers (and the underlying line renderer) are supposed to face the camera.

There are a couple ways I can think of doing this, but I'm humming over which one would be the best.

One way would be to buy a decal system (there are a couple on the asset store for under $10, at least one which is free). The tracks left via decals likely aren't the best for a car, but might be "good enough" for your usage. Also search the store for anything purpose-built for tire tracks, I suspect someone's already done it. Alternatively, you could create your own (vertex) shader to render the lines in the way that you want. You'd want to base it off the trail and line renderer system, although I can't say for sure where to start. The one (and only) time I've done this kind of thing myself was to turn a 3D mesh into a collection of lines which did face the camera. Doing the reverse would be possible with some effort, though. (Now that I think about this, I think the LineRenderer component does the mesh construction in the component before it hits the shader: that'd be the better thing to fiddle with than trying to undo that with a vertex shader. Thus, idea #3). Third, you can manually construct a triangle strip yourself. You'll have to weigh the cost/benefits of doing this over buying one off the asset store.$10 vs. 10 hours is heavily in favor of just buying it. But it shouldn't be that hard to compute the points yourself. Just remember to keep the tris slightly above the actual terrain or you'll get Z-Fighting.

• Thanks man, I think I'll give the Line Renderer a shot first, that sounds like the most doable. – mr-matt Jan 8 '16 at 21:11
• More like "duplicating the line renderer" ;) Anyway, the "hard part" will be figuring out where the four corners of each quad are. But it just takes a little geometry involving perpendicular lines to calculate ("Find the line that is perpendicular to AB and passes through C...") – Draco18s Jan 8 '16 at 21:14
• Good point! I thought I could try using raycasts under the wheel to find the exact position of the face underneath it, and then add a point on the line renderer or something, do you think that could work? – mr-matt Jan 8 '16 at 21:20
• Well, if you raycast from both sides of the wheel, you'd get a pair of points, which you could add to your custom renderer. – Draco18s Jan 8 '16 at 21:27
• That's a good idea. So would I use that as a Vector 3 position to spawn vertices or something? I'm a little confused... – mr-matt Jan 8 '16 at 21:33

The skid system I wrote works like this:

1. When in a skidding condition, every frame capture the contact point of the skid, and the normal of the surface contact point - you would probably already have this from the raycast/contact you used for your physics simulation.
2. When you have two or more points, you can start drawing the skid - to begin with just draw it as a line list or line strip so you can visualize it. See the red line and P0 to P4 in the diagram below.
3. Once you are sure you have your points in the right place, you need to create three basis vectors for each point so that you can create your polygonal data. The first basis vector is the the normal for each contact - this is your UP vector. The second basis vector is the forward vector - this is you point's position subtracted from the next point's position. Use the cross product and normalize to get the third basis vector - the RIGHT vector (or LEFT vector, depending on your coordinate system).
4. With the basis vectors you can now use the RIGHT vector to create two points, one to the RIGHT of the current point and one to the LEFT of the current point. If you were to take those points and draw them you would have to lines parallel to each other (see the black lines in the diagram below). Since the RIGHT vector is a unit vector, your two points are now P_left = P - RIGHT * half_width; P_right = P + RIGHT * half_width where half_width is the half width of the skid mark.
5. You now need to take those points and turn them into quads, or triangles (two triangles per quad; see the light-grey lines in the diagram below). How this is done depends on how you draw your data. In the end you want to stitch together each pair of points into a strip of triangles/quads.

You can see in the above diagram the black outlines of the skid mark. The points P0, P1, P2, P3, and P4 have been collected. Note that P4 has no further point so a forward vector cannot be calculated. I normally use the tire direction for the point, or simply use the same forward as the previous point. Here the blue line is the UP vector - assume that the red line lies on the X/Y plane and the Up vector is (0,0,1) - perspective is hard to show in a 2D image.

Hopefully this helps - there are some additional operations to consider to make the pinching at corners work better, but you might not have to do it if you are not turning too quickly. You will also need to consider under what conditions you need to add skids, etc.

I am pretty sure you can make the trail rendered look up instead of at the camera, and since your car is probably not going to be driving with it's right side higher than the left this could work for you.

• Welcome to GDSE. If I understand it correctly, your answer would work for flat, axis aligned terrain, but it sounds like OP needs a solution that would work with slopes, etc. – Pikalek Feb 12 at 16:02