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|%
}
}`