2014年5月14日 星期三

【程式範例】滑鼠控制攝影機旋轉及縮放

複製程式碼貼到JavaScript,拖曳到攝影機上
再將目標物件拖曳到JavaScript上的target
var target:Transform;
var xSpeed: float = 200;
var ySpeed: float = 200;
var mSpeed: float = 10;
var yMinLimit: float = -50;
var yMaxLimit: float = 50;
var distance: float = 7;
var minDistance: float = 2;
var maxDistance: float = 30;
private var x: float = 0.0;
private var y: float = 0.0;

function Start() {
    var angles: Vector3 = transform.eulerAngles;
    x = angles.y;
    y = angles.x;
}

function LateUpdate() {
    if (target) {
        if (Input.GetMouseButton(0)) {
            x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
            y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
            y = Mathf.Clamp(y, yMinLimit, yMaxLimit);
            if (x < -360) x = 0;
            if (x > 360) x = 0;
        }
        distance -= Input.GetAxis("Mouse ScrollWheel") * mSpeed;
        distance = Mathf.Clamp(distance, minDistance, maxDistance);
        var rotation: Quaternion = Quaternion.Euler(y, x, 0.0f);
        var disVector: Vector3 = new Vector3(0.0f, 0.0f, -distance);
        var position: Vector3 = rotation * disVector + target.position;
        transform.rotation = rotation;
        transform.position = position;
    }
}

0 ♥:

張貼留言