#Preventing FPS gun clipping
1 messages · Page 1 of 1 (latest)
So instead of the second camera all you will be doing is forcing the gun to render last meaning it will render on top of everything
To do that you want to make a layer called Gun and any gameobject that's supposed to render on top should be that layer
Next you see at Render there. That asset you want to open up in the inspector from your directory
In the renderer asset you should have something like this. For the Opaque Layer Mask make sure you remove Gun layer from it
After that scroll down to RendererFeatures -> Add RenderFeature -> Render Object
Then your Render Object should look exactly like this:
https://i.imgur.com/72FcjIB.png
Now, since this only uses a single camera, you do want to make it a child to the player as it moves in the same physical space
I'm also assuming the gun materials you are using are Opaque (which they should be)
Overlay cameras or forced render order will not play nice if the first person model needs to receive cast shadows from the environment
If the weapon is shrunk relative to the camera's transform, it'll move closer and less likely clip with other objects, no change in appearance from the camera's point of view
The model can additionally be moved and scaled contextually to prevent it going through objects in a rather realistic way
Lighting should be fine but yeah shadows would be a problem if the gun is inside of the wall and you want the shadow to project onto the wall it's in, but even if you shrunk that down I feel like that would be hard to portray correctly, no?
Can always just make a secondary rendering of the character that's rendered a few units back that only project shadows
Ah, yeah but if it's in the wall it does become darken unfortunately
I heard someone mention that you can do some type of matrix math to fix that, but it went over my head and I can't find it unfortunately
So shrinking the mesh would be my first option each time
Simple to do by parenting an empty to the camera with zero transforms, parenting the gun to that and then uniformly scaling down the empty
As a bonus scaling only in depth axis precisely emulates changing FoV
thank you, it works!
one small issue
you can see the insides of the gun now
Ah so it's more than one material eh
Let me think, there's a few ways about this
https://i.imgur.com/8W112mA.png
So in your materials for the gun at the bottom the sorting priority here should work for it
the problem with that is that both the insides and the top parts are the same material
Oh, yeah I see what you're talking about
so if you do that you end up with this
Right, that's a little tricky then. Well, I think Spazi's idea is probably best, but as a bandaid I'd turn event ordering to 'After Rendering' and unchecking the depth box there.
Really the more materials you have to sort against becomes a problem with this idea. For a single drawcall it should work fine but yeah I see what you're talking about running into sorting issues with inner geometry
Meaning the secondary camera idea does become more workable as you set up an environment to sort, or do as Spazi said and shrink everything and lower the near-clipping plane enough that it looks promising.
After Rendering point does work though to sort against everything on its own layer.
i got it to work!
https://www.youtube.com/watch?v=XCc6pmfy0jw i followed this tutorial
Using overlay camera to fix weapon clipping. Unity 2022 and up.
basically, you have the two cameras with different priorities, the gun camera's render type being set to "overlay". then i added it to the main camera's stack
Problem with that way is you can see the shadowing doesnt work
Only the scene lighting would apply in that case
Generally just a hard issue to resolve without some tricks unfortunately
But really I'd worry about that another time as you can always come back to this later on. Good job for getting it working though ;p
If the limitations of these techniques aren't a bother, that means they're perfect for this case
I didn't expect opaque sorting to be a problem between meshes all included in the same Render Objects though
i don't have a player model so it's actually a good thing you can't see the shadow
the shadow of a floating gun would break the immersion faster than looking down and seeing no legs
That's not the main issue, but that the gun can't receive shadows
So even if you're in shadow behind an obstacle or inside a building, you see the sun illuminating the held weapon