Orbit camera with mouse control script problems

Hi guys, I’m trying to make a third person marble platformer game and found this script online that enables me to orbit the player and move the view with the mouse but I got a problem. When the player moves behind an object the camera snaps to it’s minimum distance from the player but doesn’t snap back once the object is out of the way. This makes me have to scroll back with the mouse wheel every time it happens.

I’ve got no idea on how to fix this so any help would be greatly appreciated.

This is the script that I have:
`using UnityEngine;
using System.Collections;

[AddComponentMenu(“Camera-Control/Mouse Orbit with zoom”)]
public class MouseOrbitImproved : MonoBehaviour {

public Transform target;
public float distance = 5.0f;
public float xSpeed = 120.0f;

%|-121265975_4|%

public float yMinLimit = -20f;

%|94790627_6|%

public float distanceMin = .5f;

%|-1309372864_8|%

private Rigidbody rigidbody;

float x = 0.0f;
float y = 0.0f;

// Use this for initialization

%|1369685140_13|%
%|-1425643440_14|%
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;

    rigidbody = GetComponent<Rigidbody>();

    // Make the rigid body not change rotation

%|262759726_20|%
{
%|-1024185870_22|%
}
}

void LateUpdate () 
{

%|-803193625_27|%
{
%|566064197_29|%
y -= Input.GetAxis(“Mouse Y”) * ySpeed * 0.02f;

        y = ClampAngle(y, yMinLimit, yMaxLimit);

%|511844482_32|%

        distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);

        RaycastHit hit;
        if (Physics.Linecast (target.position, transform.position, out hit)) 

%|-2111377030_36|%
distance -= hit.distance;
%|-430734957_38|%
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
%|-1086449615_40|%

        transform.rotation = rotation;

%|1354344678_42|%
}
}

public static float ClampAngle(float angle, float min, float max)
{
    if (angle < -360F)
        angle += 360F;
    if (angle > 360F)
        angle -= 360F;

%|-1542173141_51|%
}
}`

Try using this script, it is the same version as yours, but written in javascript, and has smoothed camera movement.

#pragma strict
#pragma implicit
#pragma downcast

@script RequireComponent(Rigidbody)
 
var target : Transform;
var layermask : LayerMask;
var distance = 15.0;
var damping : float = 6.0;

var distanceMin = 2.0;
var distanceMax = 15.0;
   
var xSpeed = 120.0;
var ySpeed = 120.0;
 
var yMinLimit = -10;
var yMaxLimit = 45;
 
private var x = 0.0;
private var y = 0.0;
 
var smoothTime = 0.3;
 
private var xSmooth = 0.0;
private var ySmooth = 0.0;
private var xVelocity = 0.0;
private var yVelocity = 0.0;
 
private var posSmooth = Vector3.zero;
private var posVelocity = Vector3.zero;
 
@script AddComponentMenu("Camera Control/Mouse Orbit")
 
function Start () {
    var angles = transform.eulerAngles;
    x = angles.y;
    y = angles.x;
 
    // Make the rigid body not change the rotations.
    if (GetComponent.<Rigidbody>())
        GetComponent.<Rigidbody>().freezeRotation = true;
}
 
function LateUpdate () {              
 
    if (target){
        x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
        y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
       
        xSmooth = Mathf.SmoothDamp(xSmooth, x, xVelocity, smoothTime);
        ySmooth = Mathf.SmoothDamp(ySmooth, y, yVelocity, smoothTime);
 
        ySmooth = ClampAngle(ySmooth, yMinLimit, yMaxLimit);
 
        var rotation = Quaternion.Euler(ySmooth, xSmooth, 0);
        
		// Zoom feature using mouse wheel, and calculate the distance to the target.
		distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);
 
        // posSmooth = Vector3.SmoothDamp(posSmooth,target.position,posVelocity,smoothTime);
 
        posSmooth = target.position; // No follow smoothing.
 
        transform.rotation = rotation;
        transform.position = rotation * Vector3(0.0, 0.0, -distance) + posSmooth;
    }
   
    var hit : RaycastHit;
    if(Physics.Linecast(target.position, transform.position,hit,layermask)){
        var tempDistance = Vector3.Distance(target.position,hit.point);
        position = rotation * Vector3(0.0, 0.0, -tempDistance) + target.position;
        transform.position = position;
    }
}
 
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);
}