Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm making a game with target (orbit camera). I want to limit camera movements when in room. So that camera don't go through walls when moving around target and along the ground. What are common approaches? Any good links with tutorials? I use Unity3D, nevertheless I'm good with a general solution to the problem

share|improve this question

Usually you'll associate a collision volume with your camera, so that when it hits a wall (or floor, etc.), it stops moving. Some implementations wrap both the camera and the object it's following in the same collision volume, in order to avoid getting a wall between the camera and the object.

The physics acting on the camera collision volume should probably be different than the physics acting on the object it's following. The camera probably doesn't need to be affected by gravity, e.g.

share|improve this answer

A simple solution would be to cast a ray from the target to the desired camera position, and cap the camera position if it hits anything.

In Unity, it'd be something like this:

Vector3 targetPos = ... // this is the position of your target.
Vector3 cameraPos = ... // calculate your orbital camera position, however you choose to do it.
Vector3 targetToCamera = cameraPos - targetPos; // Vector from target to camera.

RaycastHit hit;
if (Physics.Raycast(targetPos, targetToCamera, out hit, targetToCamera.magnitude))
  // If we get this far, something is in the way.
  // Move the camera position to the collision position.
  cameraPos = hit.point;

(Please note I've not tested the code!)


This might result in some sharp camera motions, but it's a reasonable starting point. I've certainly seen commercial games use the same system!

share|improve this answer
+1 Easy to implement yet works pretty well. – JamesAMD Sep 6 '15 at 8:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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