unity3d中类似googleearth的操作

脚本放到球体 就行了
var numberAverages : int = 3;
private var originalRotation : Quaternion;
private var offsetRotation : Quaternion;
 
// Make sure there are always SphereCollider and Rigidbody
@script RequireComponent (SphereCollider)
@script RequireComponent (Rigidbody)
 
function Awake () {
    numberAverages = Mathf.Clamp (numberAverages, 1, numberAverages);
}
 
function OnMouseDown () {
    var hit : RaycastHit;
    var dir : Vector3;
 
    // Stop spinning
    rigidbody.angularVelocity = Vector3.zero;
 
    // Record initial variables
    if (Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), hit)) {
        originalRotation = transform.rotation;
        dir = hit.point - transform.position;
        offsetRotation = Quaternion.Inverse (Quaternion.LookRotation (dir));
        Spin (dir);
    }
}
 
function Spin (dir : Vector3) {
    var hit : RaycastHit;
    var privDirList : Array = new Array ();
    var currentDir : Vector3;
 
    // Initialize previous direction list
    for (var i : int = 0; i < numberAverages; i++) {
        privDirList.Add (currentDir);
    }
 
    currentDir = dir;
 
    // Make the object rotate with the cursor while we are grabbing it
    while (Input.GetButton ("Fire1") && Physics.Raycast (camera.main.ScreenPointToRay(Input.mousePosition), hit)) {
        // Remove first element of the array
        privDirList.RemoveAt (0);
 
        // Add current direction to the end
        privDirList.Add (currentDir);
        currentDir = hit.point - transform.position;
        transform.rotation =  Quaternion.LookRotation (currentDir) * offsetRotation * originalRotation;
        yield;
    }
 
    // User let go of the mouse so make the object spin on its own
    var avgPrevDir : Vector3 = Vector3.zero;
    var newPDirList : Vector3[] = privDirList.ToBuiltin(Vector3);
    for (dir in newPDirList) {
        avgPrevDir += dir;
    }
 
    // Keep spinning with the direction when you end touching
    avgPrevDir /= numberAverages;
    MoveAcceleration (currentDir, avgPrevDir);
}
 
function MoveAcceleration (r2 : Vector3, r1 : Vector3) {
    var linearVelocity : Vector3;
    var angVelocity : Vector3;
 
    // Calculate the angular velocity:  Omega = r x v / r^2
    linearVelocity = (r2 - r1) / Time.deltaTime;
    rigidbody.angularVelocity = Vector3.Cross (r2, linearVelocity) / r2.sqrMagnitude;
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值