- Home /
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?
Thanks in advance,
Answer by TowerOfBricks · Nov 28, 2009 at 05:24 PM
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?
[Edit] 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.
Thank you for the link of the script! I was looking for this for days!
The mouse orbit improved did not work for me. It was very jittery, like going back and forth between different positions rapidly.
The link to the $$anonymous$$ouseOrbitImproved script is dead, the current link is: http://wiki.unity3d.com/index.php/$$anonymous$$ouseOrbitImproved
EDIT Just to say, the script isn't working so well for me so this may not help everyone
Answer by N1nja · May 18, 2012 at 06:09 AM
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);
Answer by Tipdude501 · Nov 06, 2013 at 02:57 AM
Click on camera. Turn near clipping to .01 PROBLEM SOLVED
Not really. This may help only if the problem is that the angle to some surface makes extend beyond the near plane. However, this will not avoid colliding with objects or going through walls.
$$anonymous$$ore important, there is an important drawback to reducing near clipping: it increases the domain of the z-buffer, thus reducing its resolution, and increasing the chances of pixel z-bleeding issues in the distance.
Your answer
Follow this Question
Related Questions
iPhone frontfacing camera taking picture upon collision with another object or at a certain time? 0 Answers
how to prevent camera from moving through meshes 1 Answer
How can I change this script? 0 Answers
Modular Room System Similar to Mega Man 0 Answers
Is there a way to make the camera collide with specific objects - 2D 1 Answer