Smooth Mouse Orbit

Can anyone figure out how to add a smooth rotation to my mouse orbit script. I’d be most grateful! :slight_smile:

#pragma strict
#pragma implicit 
#pragma downcast

var target : Transform;
var layermask : LayerMask;
var distance = 25.0;
var damping : float = 6.0;

var xSpeed = 120.0;
var ySpeed = 120.0;

var yMinLimit = -10;
var yMaxLimit = 75;

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

@script AddComponentMenu("Camera-Control/Mouse Orbit")

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

SOLVED: The mouse rotation 3rd person camera script with smooth rotation and collision. :smile:

#pragma strict
#pragma implicit 
#pragma downcast

var target : Transform;
var layermask : LayerMask;
var distance = 25.0;
var damping : float = 6.0;

var xSpeed = 120.0;
var ySpeed = 120.0;

var yMinLimit = -10;
var yMaxLimit = 75;

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 rotation
   	if (rigidbody)
		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);
 
       // 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);
}

add this to the script because if you move your mouse up or don oo much making the y go too high or low, it will tkae a while for it to come back to where it’ll move.

if(y > yMaxLimit)
y = yMaxLimit;

if(y < yMinLimit)
y = yMinLimit;