0
\$\begingroup\$

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.

enter image description here

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).

enter image description here

\$\endgroup\$
  • \$\begingroup\$ This is unusual. Have you disabled depth testing on your laser sight, or rendered your columns with a shader that includes transparency? Normally opaque geometry is drawn first, and then transparent content will sort correctly against it using the depth buffer. Sorting conflicts typically only arise between to transparent objects, since when using blending they can't rely on depth writing to sort them out. \$\endgroup\$ – DMGregory Apr 4 '17 at 16:58
1
\$\begingroup\$

UPDATE: doesn't work entierly!

I solved this by setting the camera's transparency sort mode to orthgraphic:

Camera.main.transparencySortMode = TransparencySortMode.Orthographic;

Works for both LineRenderer or using a deformed cube.

https://docs.unity3d.com/ScriptReference/TransparencySortMode.html

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.