I'm using a line renderer as a laser aim for my character. If the aim hits an object it ends at that object. If the aim doesn't hit an object it continues a predefined distance.
If I aim at an object it works fine, if I stand behind the object it also works, but if I stand in front of it, the line is rendered behind the object creating the illussion that the character stands behind the object.
If I change the sorting order for the line I can make it to be in front of the object, but this is a bit tedious because then it will be in front of this object when it is in fact behind it.
In the screenshot below a line is drawn from the picked up C4 which is in front of the pillar, but the line is drawn behind the pillar.
Is there any way to solve this issue with a line renderer? I've been thinking of just using a cube and stretch it instead.
Thanks in advance!
Update 1: I made a cube-solution instead, but soon realized that the issue is with a shader that is set to fade/transparent and that this is due to sorting order. How can I dynamically change the sorting order in code so that my moving transparent object always appear in the correct place?
Update 2: I noticed that my own answer below isn't working 100%. It works for most scenarios but if I'm standing at origo and aiming behind a pillar the sight is rendered in front of the pillar. In the image below it is clearly visible from the top view that I'm aiming behind the pillar. (This is done with a deformed cube, the line renderer had even more issues).