Camera Collider Help

I'm using the following camera code from Unitylabs: http://www.unitylabs.net/tutorials/gameplay-mechanics/third-person-camera-controller

But the tutorial was never finished. Could someone tell me how to make it to where it won't collide with the walls and will rotate to a more suitable direction.

@script AddComponentMenu("Camera-Control/Camera Controller")

var target : Transform;
private var distance = 2.0;
private var xSpeed = 250.0;
private var ySpeed = 120.0;
private var yMinLimit = 0;
private var yMaxLimit = 80;
private var cameraMode = "third";
private var zoomMinLimit = 2;
private var zoomMaxLimit = 6;

private var x = 0.0;
private var y = 0.0;

function Start ()
{
    var angles = transform.eulerAngles;
    x = angles.y;
    y = angles.x;
}

function LateUpdate ()
{

    if(cameraMode == "third")
    {
        var characterRotation = target.transform.eulerAngles.y;
        var cameraRotation = transform.eulerAngles.y;

        if(Input.GetButton("CameraZoomIn") && distance > zoomMinLimit)
        {
            distance = distance -1 / (xSpeed * 0.02);
        }
        if(Input.GetButton("CameraZoomOut") && distance < zoomMaxLimit)
        {
            distance = distance +1 / (xSpeed * 0.02);
        }

        x += Input.GetAxis("CameraX") * xSpeed * 0.02;
        y -= Input.GetAxis("CameraY") * ySpeed * 0.02;

        y = ClampAngle(y, yMinLimit, yMaxLimit);

        if(Input.GetAxis("Vertical") || Input.GetAxis("Horizontal"))
        {
            rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(y, characterRotation, 0), Time.deltaTime * 3);
            position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
            x = characterRotation;
        }
        else
        {
            rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(y, x, 0), Time.deltaTime * 3);
            position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
        }

        transform.rotation = rotation;

    transform.position = position;

    }
    else
    {

    }

}

static function ClampAngle (angle : float, min : float, max : float)
{
    if (angle < -360)
    {
        angle += 360;
    }
    if (angle > 360)
    {
        angle -= 360;
    }

    return Mathf.Clamp (angle, min, max);
}

If you want it to not collide with walls, you could easily go into your physics settings and uncheck collisions between your camera layer and the layers you don't want it to collide with. You could also use Physics.ignoreCollision and/or Physics.ignoreLayerCollision if you want to change camera collisions at run-time.

I'm not sure why you would ask for this since the tutorial never implemented collisions, but that will "make it to where it won't collide with the walls" as you aksed for.

If you wanted to finish the tutorial:

  1. An obvious solution to implement camera collisions would be something like adding a sphere collider and rigidbody without gravity or a characterContoller to your camera and setting it to ignore collisions with your player layer and anything else you want it to pass through, but these would require a fair amount of rework and you would also have to add catches for situations where the camera might get stuck.
  2. A smarter solution that is less work is to do a spherecast and adjust your camera position based on any collisions. You could also do selective raycasting, but that is actually more work , even if it may be somewhat more efficient.

There are other ways to implement camera collisions and the best solution would be determined by your use case.