Unity Camera Going Through Walls? Fix Third-Person Camera Clipping

Illustration: a camera icon bumping against a wall with a protective glowing bubble

You back the player against a wall and the camera slides straight through it, showing the void behind the level. Or it dips under the floor on slopes, or pokes through a doorway. Unity camera clipping through walls is a classic third-person problem, and the fix is the same idea every time: stop the camera from sitting inside geometry. This guide shows the Cinemachine 3 route first (Deoccluder, Decollider and the Third Person Follow collision settings), then a manual spherecast version, and then the layer and near clip plane settings that make either one behave.

Why the Camera Clips

A camera is just a point with a view. Nothing stops it moving into a wall unless you add a rule. The camera sits behind and above the player, so when the player stands close to a wall the ideal camera position is inside or behind that wall. Two different problems are mixed up in the word “clipping”:

  • The camera itself is inside geometry. You see the back faces of walls or the inside of the ground. The fix is to push the camera out.
  • Geometry sits between the camera and the player. The player is hidden by a wall or a tree. The fix is to move the camera in front of the obstacle, or fade it.
  • The near clip plane cuts the wall. The camera is close to a surface and the plane slices it open, so you see through it.

Cinemachine has a different tool for each. If you do not have a third-person rig yet, start with the Cinemachine 3 third-person camera guide.

Third Person Follow Collision

If your CinemachineCamera uses the Third Person Follow position control, it has basic collision handling built in. Select the CinemachineCamera and look at the component’s collision fields:

  • Camera Collision Filter is the layer mask of things the camera should not pass through. Include your level geometry. Exclude the player’s own layer, or the camera will pull into the character.
  • Camera Radius is the size of the sphere used to test for walls. A larger radius keeps the camera further from surfaces and avoids slicing through corners.
  • Damping Into Collision and Damping From Collision control how quickly the camera moves in when it hits something and back out afterwards. Small values react fast. Large values smooth out the movement.
  • Ignore Tag lets you skip objects by tag, such as the player.

Try this first. Set the filter to your environment layers, Camera Radius to about 0.2, and a short damping. This alone fixes many simple levels.

Cinemachine Deoccluder

For more control, add the CinemachineDeoccluder extension. Select the CinemachineCamera and use the Add Extension dropdown in the Inspector. The Deoccluder checks the line from the camera to the target and moves the camera when something blocks it.

Fields worth knowing, as they appear in the Inspector:

  • Collide Against: the layers that count as obstacles. Do not include the player.
  • Transparent Layers: layers the camera can see through, such as glass or grass, so they never block.
  • Minimum Distance From Target: stops the camera collapsing right into the player’s head.
  • Camera Radius: the thickness of the collision test.
  • Strategy: how the camera moves when blocked. Different strategies push the body, preserve its height or preserve its distance from the target. Try each and see which suits your game.
  • Smoothing Time and Damping: how quickly the camera moves out of the way and back.

A third-person action game usually wants a Collide Against mask of Default and Environment, a radius around 0.2 to 0.3 and a Strategy that keeps the camera at a sensible height, so it does not swing wildly over the player’s head. If the camera shakes or stutters while avoiding walls, also read the Cinemachine camera jitter guide.

Cinemachine Decollider

The CinemachineDecollider extension solves the other problem: it makes sure the camera itself is not inside colliders. It pushes the camera out of geometry and can keep it above terrain. This is the one that stops the camera dipping under the floor on slopes and into thin walls.

Add it the same way, with Add Extension on the CinemachineCamera. Set its camera radius, choose the layers it should avoid, and enable its terrain handling if your level uses Unity Terrain. The Decollider and Deoccluder work well together: Deoccluder keeps the view of the player clear, Decollider keeps the lens out of solid objects. Order matters a little, and you can test the combined effect by walking into corners in Play mode.

Both extensions use physics queries, so the colliders they test against must really exist. A mesh without a collider cannot stop the camera. If walls have no collider, the player would walk through them as well, which usually makes this obvious.

A Manual Spherecast Camera

If you do not use Cinemachine, a spherecast does the same job in a few lines. This camera sits on a child of a pivot, which follows the player and rotates with mouse look. The camera casts a sphere backwards from the pivot, and stops at the first wall.

using UnityEngine;

public class CameraCollision : MonoBehaviour
{
    [SerializeField] Transform pivot;          // the point the camera orbits (e.g. shoulder height)
    [SerializeField] float desiredDistance = 4f;
    [SerializeField] float minDistance = 0.6f;
    [SerializeField] float radius = 0.25f;
    [SerializeField] float skin = 0.1f;
    [SerializeField] LayerMask obstructionMask;
    [SerializeField] float smoothTime = 0.05f;

    float currentDistance;
    float velocity;

    void Start() => currentDistance = desiredDistance;

    void LateUpdate()
    {
        Vector3 dir = -pivot.forward;
        float targetDistance = desiredDistance;

        if (Physics.SphereCast(pivot.position, radius, dir, out RaycastHit hit,
                desiredDistance, obstructionMask, QueryTriggerInteraction.Ignore))
        {
            targetDistance = Mathf.Max(hit.distance - skin, minDistance);
        }

        // snap in quickly, ease back out
        float time = targetDistance < currentDistance ? 0f : smoothTime;
        currentDistance = time == 0f
            ? targetDistance
            : Mathf.SmoothDamp(currentDistance, targetDistance, ref velocity, time);

        transform.position = pivot.position + dir * currentDistance;
        transform.rotation = pivot.rotation;
    }
}

Key points:

  • Run it in LateUpdate, after the player and pivot have moved.
  • The camera moves in instantly when blocked, because a laggy camera would clip for a few frames, and eases back out, so it does not pop.
  • The obstruction mask must not include the player. Otherwise the cast hits the player’s own collider and the camera sits inside their head.
  • QueryTriggerInteraction.Ignore stops trigger zones from pushing the camera. If you are not sure how masks and queries work, the raycast tutorial explains them.

A plain Physics.Raycast is not enough here. A ray has no thickness, so the camera can sit right against a wall and the near plane still cuts through it. The sphere gives the camera some body.

Near Clip Plane

Even with collision, you can see through wall surfaces when the camera is close to them. This is the camera’s Near Clip Plane. On a regular Camera it is in the Inspector, and on a Cinemachine setup it is in the Lens settings of the CinemachineCamera. The default is 0.3.

  • Lower the value to 0.1 or 0.05 to see less cut-away geometry.
  • Do not set it extremely low. Near and far planes share the depth buffer, and a tiny near plane with a large far plane causes flickering and z-fighting in the distance.
  • Better still, keep the camera radius larger than the near plane. If the camera never gets closer to a wall than its radius, the plane never cuts the wall.

Layers and Colliders

Most “the camera ignores my wall” bugs are layer bugs.

  • Put level geometry on a clear layer such as Environment, and use that in every camera collision mask.
  • Do not include Player, Enemies and small props. Small props that bump the camera cause irritating jerks.
  • Walls need colliders. A thin plane with a MeshCollider only collides from one side, so check the normals.
  • Trigger colliders are ignored by default in the manual version. Make sure invisible trigger volumes are not part of the mask in Cinemachine either.
  • For a matrix of which layers interact, see the collision layers matrix guide.

🔧 Camera tools

Third-person cameras take a lot of tuning. Our list of Asset Store tools for indie devs includes helpers that save hours.

Troubleshooting

The camera still goes through thin walls

The wall may have no collider, or it is thinner than the camera moves in a single frame. Add a collider, give it some thickness, and use the Decollider as a safety net.

The camera is stuck inside the player’s head

The collision mask includes the player’s layer. Remove it, or add the player’s tag to Ignore Tag where available.

The camera jitters or pops in and out near walls

The radius is too small or there is not enough damping, so the camera flips between hit and no hit. Increase the radius and the smoothing time.

The camera shows the void under the floor on slopes

Add the Decollider, include the ground layer, and set a radius large enough for the slope angle.

I can see through walls only when very close

That is the near clip plane. Lower it a little and raise the camera radius so the camera stays further from walls.

The camera gets pushed by invisible objects

Trigger volumes or props on a layer in the mask are blocking it. Narrow the mask to level geometry only.

FAQ

Do I need both the Deoccluder and the Decollider?

Not always. Deoccluder keeps the player visible. Decollider keeps the camera out of solid objects. Many games use both.

Is this the same in 2D?

2D cameras are orthographic and sit in front of the scene, so they do not clip into walls. This problem is for 3D and perspective cameras.

Can I fade walls instead of moving the camera?

Yes. Cast from the camera to the player and set hit renderers to a transparent material. Many games combine fading and collision.

Is a spherecast expensive?

One cast per frame is cheap. It is fine for a player camera.

Why does it work in the editor but not in a build?

Check that your layer masks and tags are saved on prefabs. Overrides on scene instances can be lost on prefabs that are instantiated at runtime.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *