Camera Collision Help Thanks In Advance

Hello, all!

I have been making a developing a game with my friends, and we are pretty early into the development stage. I am one of the 3 programmers and we have been working on gameplay lately. But I recently noticed that the camera is going through walls and we need it fixed, but I don’t know what to do.

But here’s what I have tried:

I attached a collider component (didn’t work)
I attached a rigidbody component (didn’t work)
I attached made a new GameObject with a collider and rigidbody
and had the camera be a child of it and vica versa
I also made a C# script to make the camera follow the script:

using UnityEngine;
using System.Collections.Generic;

public class FollowCamera : MonoBehaviour
    public Transform CameraObject;
    using UnityEngine;
    using System.Collections.Generic;
    public class FollowCamera : MonoBehaviour
        public Transform CameraObject;
        void Update()
            // Move this transform towards the camera object.
            transform.Set(CameraObject.position.z, CameraObject.position.y,


The mouse orbit script I used looks like this:

var target : Transform;
var distance = 10.0;
var height = 5.0;

var xSpeed = 250.0;
var ySpeed = 120.0;

var yMinLimit = -20;
var yMaxLimit = 80;

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

var heightDamping = 2.0;
var rotationDamping = 3.0;

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

	// Make the rigid body not change rotation
   	if (rigidbody)
		rigidbody.freezeRotation = true;

function LateUpdate () {

	if (Input.GetMouseButton(1))
	    if (target) {
	        x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
	        y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
	 		y = ClampAngle(y, yMinLimit, yMaxLimit);
	        var rotation = Quaternion.Euler(y, x, 0);
	        var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
	        transform.rotation = rotation;
	        transform.position = position;
	    	var wantedRotationAngle = target.eulerAngles.y;
			var wantedHeight = target.position.y + height;
			var currentRotationAngle = transform.eulerAngles.y;
			var currentHeight = transform.position.y;
			currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
			currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
			var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
			transform.position = target.position;
			transform.position -= currentRotation * Vector3.forward * distance;
			transform.position.y = currentHeight;

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

It looks like this:

But still nothing works. :frowning:

Any suggestions? And thanks in advance!



I have realized since the first post it was kind of vague so I included a screen shot of the game and attached the mouse orbit script I was using.

You can try ray casting (or probably spherecasting) from the cameras location to the thing it is focussed on. If the raycast doesn’t hit the focussed object first then leave the camera where it was in the last frame (both position and rotation). I normally allow a number of different potential camera angles and if one remains unavailable for a few frames I switch to another valid one.

Other techniques include approaching the focussed object by reducing the distance from the camera if it is currently obscured or changing the angle of the camera towards an overhead view