Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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
add comment

2 Answers

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!)

See http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

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
add comment

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
add comment

Your Answer

 
discard

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.