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|% } }
press the 101010 button and then paste the code in to make it a lot easier to read
I did, dunno why it came out this way. it was looking fine when posted...`using UnityEngine; using System.Collections;
[AddComponent$$anonymous$$enu("Camera-Control/$$anonymous$$ouse Orbit with zoom")] public class $$anonymous$$ouseOrbitImproved : $$anonymous$$onoBehaviour { %|21353932_1|% %|2119513985_2|% public float distance = 5.0f; %|-1436610347_4|% public float ySpeed = 50f; %|-2045021080_6|% %|1526089662_7|% %|581643164_8|% %|1678793086_9|% %|1882850125_10|% %|-375249229_11|% %|-1675583003_12|% private Rigidbody rigidbody;
%|32840933_15|% float y = 0.0f; %|541982102_17|% %|1253687667_18|% %|818444616_19|% %|1581547236_20|% %|-996125286_21|% %|821362789_22|% %|-1243507389_23|% %|64174672_24|% %|-1616665804_25|% %|1648709565_26|% %|2060026496_27|% %|1175160055_28|% %|308302270_29|% %|-1656012909_30|% } %|-273750130_32|% %|1995535531_33|% %|1262513946_34|% { if (target) %|-310352386_37|% %|-2107229183_38|% %|-484912158_39|%
%|-2135468032_41|%
Quaternion rotation = Quaternion.Euler(y, x, 0);
%|1848528464_44|% %|-866202068_45|% %|523656513_46|% %|-240059982_47|% if (Physics.Linecast (target.position, transform.position, out hit)) %|-1281019538_49|% %|-1741625481_50|% } %|1056605878_52|% %|-1328614516_53|% %|-648635059_54|% %|-722956583_55|% %|2118233532_56|% } %|1484113543_58|% %|848806890_59|% %|-1282872457_60|% %|-71424169_61|% %|1203695871_62|% %|-654435889_63|% %|1078620462_64|% %|-1848865403_65|% %|-142699574_66|% %|-1653633932_67|% }`
using UnityEngine; using System.Collections;
[AddComponent$$anonymous$$enu("Camera-Control/$$anonymous$$ouse Orbit with zoom")] public class $$anonymous$$ouseOrbitImproved : $$anonymous$$onoBehaviour {
public Transform target;
public float distance = 5.0f;
public float xSpeed = 50f;
public float ySpeed = 50f;
public float y$$anonymous$$inLimit = -1f;
public float y$$anonymous$$axLimit = 80f;
public float distance$$anonymous$$in = 2f;
public float distance$$anonymous$$ax = 15f;
private Rigidbody rigidbody;
float x = 0.0f;
float y = 0.0f;
// Use this for initialization
void Start ()
{
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
rigidbody = GetComponent<Rigidbody>();
// $$anonymous$$ake the rigid body not change rotation
if (rigidbody != null)
{
rigidbody.freezeRotation = true;
}
}
void LateUpdate ()
{
if (target)
{
x += Input.GetAxis("$$anonymous$$ouse X") * xSpeed * distance * 0.02f;
y -= Input.GetAxis("$$anonymous$$ouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, y$$anonymous$$inLimit, y$$anonymous$$axLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
distance = $$anonymous$$athf.Clamp(distance - Input.GetAxis("$$anonymous$$ouse ScrollWheel")*5, distance$$anonymous$$in, distance$$anonymous$$ax);
RaycastHit hit;
if (Physics.Linecast (target.position, transform.position, out hit))
{
distance -= hit.distance;
}
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
public static float ClampAngle(float angle, float $$anonymous$$, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return $$anonymous$$athf.Clamp(angle, $$anonymous$$, max);
}
}
Answer by yogihermawan1993 · Dec 12, 2015 at 04:04 AM
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);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612045828im_/https://answers.unity.com/themes/thub/images/avi.jpg)