Game Development is full of surprises. Completely different problems can be solved with the help of a pretty similar solutions. Let’s take two examples:
- Post Effects. This is a very powerful mechanism of adjusting the rendered picture right before displaying it on the screen. Using the effects it is pretty easy to add the fog or increase the brightness, etc. But in case if the game screen displays not only the environment but also some UI elements (health bar, number of points, etc) it turns to be a problem to apply the effects only to the particular part of the picture skipping UI.
- First Person Shooter. Having first person view in the game the player always sees own gun. This gun is usually attached to the player’s model. The model itself has a collider that interacts with the environment (detects collisions and prohibits passing through the walls, etc). In case if the player has to come up to the wall very closely it can happen that the gun can pass through the wall. Having a separate collider attached to the gun would not solve the problem since it would block the player from coming close to the wall.
But how to deal with these, at first sight, different problems?
First, it is required to understand the common problem between those two examples. In general it sounds like this: one part of the screen has to be rendered independently from another. Having in mind the fact that the resulting picture on the screen can consist of multiple cameras, the solutions seems pretty obvious. And in fact it is, the only thing that might be tricky here is a configuration of cameras.
Here is a visual representation of the second example.
Step 1. The player stays far enough from the wall
Step 2. The player comes closer to the wall. The gun is clipping through the wall now
In general, the solution would be the following:
- One camera (first) renders environment only (without the gun).
- Another camera (second) renders the gun only.
- The order of rendering is defined by the camera number.
- A combined picture will have a gun always on top of the environment.
Done, now it time to make a proper configuration.
At this moment there is only one camera on the scene: it is attached to the player to follow him. Additionally, it has no specific configuration. The second camera can be attached to the player and positioned in the same way as the first one.
Now the configuration. There are three important properties that will help to implement the solution:
Since “Culling Mask” is working with layers it is required to put a gun object into a separate layer, for example into a new one called “Weapon”.
Player Camera Settings
This camera must be drawn at first, thus it can use skybox. But it shouldn’t draw a gun since it will be drawn later.
|Clear Flag||Skybox/Solid Color|
|Culling Mask||All layers except of the new one “Weapon”|
Gun Camera Settings
This camera must be drawn at last, keeping empty space transparent so underlying layers can be still visible. The only layer it should draw is “Weapon”.
|Clear Flag||Depth Only|
|Culling Mask||“Weapon” layer only|
In the editor (right side of the image) the gun is still clipping through the wall, but in the game (right side of the image) everything looks fine.
11,378 total views, 4 views today