# Camera collision

Hi,

I am trying to understand camera collision in Unity but their is one thing that I can't seem to solve:

When I turn my camera against a gameObject like a cube or other primitive, the collision isn't detected.

I tried almost everything like linking a collidable object to a camera, adding a mesh collider or rigid body to it but the camera stays on going through walls when I turn with it.

Any idea's how to solve this?

To make something collideable you need to attach a collider to it and a rigidbody if it should be moving in the game.

Although cameras aren't usually collidable since the scripts moving them will most likely don't use the physics engine.

What script are you using to move the camera?

 Aha, mouse orbit, well the mouse orbit script does change the position of the camera every frame regardless of the physics, so you won't be able to have a camera with that script which responds to collisions, to avoid making the camera go through objects you can use a modified mouse orbit script which can be found on the wiki: http://www.unifycommunity.com/wiki/index.php?title=MouseOrbitImproved It will look for it there is a wall between the target and the camera and if so, shorten the distance to the target to make you get a clear view.

have i got an awesome prefab for you:-

http://blockmonkey.com/my-work/unity-3d-work/

make sure gravity is off, and also make sure that you freeze rotation, also… its going to push through walls unless you place all that code inside a FixedUpdate, at least these are the things I did to improve mine… however it still snaps a little, but not related to the camera… related to the pivot its fallowing;

``````		theta = Mathf.Clamp(theta, -35, 20);
phi = Mathf.Clamp(phi, -90, 90);
targetRotation = Quaternion.Euler(theta, phi, 0);

float dis = (pivotObject.transform.parent.position - transform.position).magnitude;
pivotObject.transform.parent.renderer.enabled = !(dis < hidePlayerDistance);

RaycastHit hit;
cDistance = Mathf.Lerp(cDistance, maxDistance, lerpSpeed*Time.deltaTime);
if( Physics.Linecast( transform.position, pivotObject.transform.position, out hit, layerMask.value) ) {
cDistance -= hit.distance + (sphereCollider.radius * 2);
}
cDistance = Mathf.Clamp(cDistance, 0, maxDistance);

targetPosition = targetRotation * (pivotObject.transform.forward * -cDistance) + pivotObject.transform.position;
transform.position = Vector3.Lerp(transform.position, targetPosition, lerpSpeed*Time.deltaTime);
transform.LookAt(pivotObject.transform);
``````

Click on camera. Turn near clipping to .01
PROBLEM SOLVED

i have a problem when i type GameCamera it goes red and i can’t add the script to the main camera any help

#pragma strict

``````function Start () {

}
var colide = 0;
var inward = 0;
function Update () {
if (colide==1) {
transform.Translate(0, -0.05, 0.5);
inward = inward + 5;
} else if (0 <= inward){
transform.Translate(0, 0.01, -0.1);
inward--;
}
}
function OnTriggerEnter (other : Collider) {
colide = 1;
}
function OnTriggerExit(other: Collider) {
colide = 0;
}
``````

This worked for me

TYVM Block_Boy_ Worked for me too, added it to MainCamera as Javascript, does the work with MouseOrbitImproved, was looking for this fix for almost a day or something