How can I prevent my camera from clipping into the terrain in my RPG project?

As you can see in my picture, if the Player is walking right along the edge of the terrain, the camera seems to pass through the terrain, and the Player is able to see outside of the designed environment. I’m
not sure if this is touched on later in the course, but this seem like a good time to ask if anyone had suggestions on how to address this problem. Thanks!

1 Like

One of the simplest things you can do is to design your levels in such a way that the player never actually approaches the boundary in the first place.

This can be done with tricks like creating neighbor terrains with enough detail to infer further scenery (and a portal at the edge of your main terrain to move to a scene with that neighbor terrain as the main terrain). Another important trick is in the layout of your buildings, etc.
In television and movies, sets are built in such a way that when you look at them through the camera, they look like you’re inside a room or deep in the woods with nobody around, when in truth, just on the other side of the camera the 4th wall is cameras and producers and sound folks, etc. Everything is arranged so that those 4th wall details are hidden from view. In the same way, we need to construct our scenes so that everywhere the player can go gives an unobstructed view of the player without buildings blocking the way or the illusion of a big open world destroyed by reaching the end of the terrain.

2 Likes

Thank you Brian! I’m going to edit my level using your suggestions. I think I am going to create some bordering terrains like you mentioned. When I am completed with the first moment, perhaps I can design one of the neighboring terrains to create a seamless transition between scenes like you mention. Thanks!


Hi Brian, Ive been editing my scene a bit to solve the issue. One issue I ran into while creating additional scenery to suggest an extended world was creating areas of the terrain that are not accessible to the Player. If I made my terrain too high, the camera would clip through it. Too low and when I baked the navmesh the player could walk on it. So, an idea I had was to place objects in the scene while editing, making them navigation static, and baking the nav mesh. Then, I attached simple scripts to them that would destroy these objects immediately at the start of the game. That way, they are no longer there but the area of the terrain they occupied cannot be walked on by the player. This approached seems to work but doesn’t really feel that efficient. The first picture I attached is my scene in edit mode, while the second is my scene during play. I wanted to ask your general thoughts on this approach or if there is an easier way to dictate parts of the terrain as not navigable by the Player. Thanks!

1 Like

That’s pretty much what I would do as well.

1 Like

This approached seems to work but doesn’t really feel that efficient.

Why not simply make invisible walls at the border, rather than creating and destroying objects?

Edit: i have absolutely no idea if this is even possible, and I can see about three ways it could go wrong, but if you can attach something to your virtual camera that can interact with tags/objects, you could possibly add a tag to the walls and script it so that the camera cannot pass through them. It would limit rotation when the player is near the edge, but it would also be a way to keep them from looking where you don’t want.

1 Like

Thanks for the suggestions!Before trying my approach, I tried to create empty game objects with box colliders, but for some reason when baking the na mesh it did. not take them into account, even with them set as navigation static. Im not really sure why.

The version of the NavMesh being used by default doesn’t take colliders into account, but actually uses scene geometry.

There is a slightly more complex version of the NavMesh system which on newer versions of Unity can be installed through the package manager. This version completely replaces the existing one. Your Navmesh surface is a component on a GameObject within the scene, and it has options to consider scene geometry or colliders.

2 Likes

Thanks for explaining that!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms