Tiled line renderer material - i don't want it to stretch

The question is simple enough.

I’m making a 2D laser for a sidescrolling shooter, and it goes like this:

I’m using a Line Renderer which uses its own position (on the player) as a start position. Its end position is childed with an empty end object which is off screen, so it moves together with the start object and they always form a straight line. 2D Ray is cast to detect hits, and the end position has its position set every frame on the detected hit position so the line rendering stops there giving the illusion of the laser hiting an object. When raycast is no longer detecting a hit, object defining the end of the line returns back to predefined X position, so it’s all nice and dandy.

However, Line Renderer is using Particles/Additive material with 1/1 tiling and its offset being modified every frame to make an illusion of movement. When i’m moving closer to the object hit, the material on the line is shrinking, and when i’m moving away it gets stretched.

How can i always have the fixed size of the material applied to Line Renderer regardless off the line length? I’ve been googling for two days, similar topic have been around, but i haven’t found any appropriate answers. I suppose i could change material tiling on runtime, but i don’t know what parameters i should use to scale correctly. Besides, i suppose there must be a simpler way.

It’s been an annoyance with the line and trail renderer for a while now. The solution is to set the material’s texture scale dynamically via script. The other solution is to not use Unity’s line renderer at all and write your own (or use one from the asset store or modify a trail renderer from the community wiki, nearly all of which add this feature).

1 Like

Thanks for the answer, looks like i’ll have to use a third party solution, i’m not very good at scripting of that level. It’s a shame that there isn’t a simple tick to prevent the texture from scaling, which seems like a logical thing to be supported.

In Unity 5.5, we added a Texture Mode option to lines and trails, to solve this problem.

I believe we have an additional feature request to reverse the U direction, to make the texture appear totally static in the world. However, even without this extra feature, maybe the new Texture Mode solves your problem… give it a go, if you’re using 5.5!

3 Likes

Also, maybe this will help, in you are not using 5.5:

1 Like

@richardkettlewell

Hello and thank for the answer! As for the video, i’ve seen it, but it’s too low res to see the code, and i’m afraid i’m not quite apt at scripting to make it out by listening only (though i’m aware what should i do in theory).

It’s not critical right now and 5.5 is quite new (i’m still on 5.3.5) so i’m afraid i’ll break some assets if i upgrade right now, but it’s good that the function is implemented and i’ll give it a go when the time comes to polish things up.

Ok, good luck! :slight_smile:

And sorry about the link… I didn’t check the resolution, and foolishly assumed that it would have been uploaded at a legible resolution! :smile:

No problem, appreciate the help :slight_smile:

1 Like

@richardkettlewell

Just a small follow-up, i recently updated to 5.5.0f3 and the texture mode option on line renderers is very nice, does what i needed :slight_smile:

1 Like

The feature to reverse U direction to have the texture appear static would be great! For now the tiled texture mode will do, thanks! In the meantime, I’ll have to check if it is possible to make my own script to make the texture seem static, though I’m not sure I have the skills necessary.

Cheers!

here is one more example from Japanese guys https://unity3d.com/jp/learn/tutorials/projects/2d-shooting-game/creating-the-background?playlist=46524 instead of Renderer use LineRenderer type, I guesses. And don’t forget set “Repeat” for your texture

Thanks, you are great!

1 Like

Hi there!
For anyone wondering a simple non-scripting solution to that problem simply create a shader graph and add a tiling property as a vector2 and manually tweak it with your desired texture. Apply the shader to a material and the material to the line renderer. Set the line renderer Texture Mode to Tile and hit save.

Hope it helps!

1 Like

Solution is simple - enable tiling for Line Renderer, choose wrap mode - repeat in texture import params and set Corner Vertices (Line renderer param in ’ positions’ group) greater than zero

1 Like

Corner Vertices = 1 did it for me. Thanks