x


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,

more ▼

asked Nov 28 '09 at 03:45 PM

user-388 (google) gravatar image

user-388 (google)
-8 1 1 2

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered Nov 28 '09 at 05:24 PM

TowerOfBricks gravatar image

TowerOfBricks
3.2k 17 25 50

Thank you for the link of the script! I was looking for this for days!

Apr 07 '12 at 07:39 PM PotatoOrgyLt

The mouse orbit improved did not work for me. It was very jittery, like going back and forth between different positions rapidly.

Dec 01 '12 at 12:26 AM create3dgames
(comments are locked)
10|3000 characters needed characters left

have i got an awesome prefab for you:-

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

more ▼

answered Feb 24 '11 at 12:11 PM

angad singh gravatar image

angad singh
25 5 5 11

(comments are locked)
10|3000 characters needed characters left

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);
more ▼

answered May 18 '12 at 06:09 AM

N1nja gravatar image

N1nja
445 22 28 37

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3010
x2499

asked: Nov 28 '09 at 03:45 PM

Seen: 9684 times

Last Updated: Dec 01 '12 at 12:26 AM