All pastes #2123353 Raw Edit

Someone

public text v1 · immutable
#2123353 ·published 2012-03-01 22:57 UTC
rendered paste body
#pragma strict

/*var minFov: float = 15f;
var maxFov: float = 90f;
var sensitivity: float = 10f;
var cameramain : Transform;
var lastMousePosition : Vector3;

function Zooming(){

var fov: float = Camera.main.fieldOfView;
  fov += Input.GetAxis("Mouse ScrollWheel") * sensitivity;
  fov = Mathf.Clamp(fov, minFov, maxFov);
  Camera.main.fieldOfView = fov;

}

function Update () {
  
Zooming();

}
*/
var target : Transform;
var distance = 4.5;


var xSpeed = 250.0;
var ySpeed = 120.0;


var yMinLimit = -20;
var yMaxLimit = 80;
 

var zoomRate = 20;


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

private var startPosition : Vector3;

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


function Start () {

    var angles = transform.eulerAngles;

    x = angles.y;

    y = angles.x;
    
    startPosition = transform.position;


    if (rigidbody)

        rigidbody.freezeRotation = true;

}

 

function LateUpdate () {

 

    if (target) {

        if (Input.GetMouseButton(1))

        {

        x += Input.GetAxis("Mouse X") * xSpeed * 0.02;

        y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;

        var test = 0;

        test = y;

        }

        distance += -(Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime) * zoomRate * Mathf.Abs(distance);

        if (distance < 2.5)

        {

            distance = 2.5;

        }

        if (distance > 10)

        {

            distance = 10;

        }

        

        

        y = ClampAngle(y, yMinLimit, yMaxLimit);

        if( y == yMinLimit && test == yMinLimit)

        {

            // This is to allow the camera to slide across the bottom if the player is too low in the y 

            distance += -(Input.GetAxis("Mouse Y") * Time.deltaTime) * 10 * Mathf.Abs(distance);

        }

        

        var rotation = Quaternion.Euler(y, x, 0);

        var position = rotation * Vector3(0.0, 2.0, -distance) + target.position;

        transform.rotation = rotation;

        transform.position = position;

    }
    
     if (Input.GetMouseButtonUp(1)) {

        transform.position = startPosition;
        

    }
    }


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

}