2014-06-06 63 views
0

這是我的腳本(用JavaScript編寫),它創建相機移動。我無法弄清楚如何做到這一點,以便我能讓它不會太靠近對象(目標)。這可能是簡單的事情,但我是Unity的小菜鳥。停止我的相機關閉

如果通過修改現有代碼無法獲得它,您能否提出另一種獲得所需運動的方法?

#pragma strict 
var target : Transform; 
var localOffset = Vector3(0,0,0); 
var finalPos : Vector3; 
var currentPos : Vector3; 
var speed = 0.01; 
private var startTime: float; 
private var journeyLength: float; 

function Start() { 
    startTime = Time.time; 
} 

function Update() { 
    currentPos = transform.position; 
    finalPos = target.position + (target.up * localOffset.z); 
    finalPos = target.position + (target.forward * localOffset.x); 
    transform.LookAt(target.position); 

    journeyLength = Vector3.Distance(transform.position, finalPos); 
    var distCovered = (Time.time - startTime) * speed; 
    var fracJourney = distCovered/journeyLength; 

    transform.position = Vector3.Lerp(transform.position, finalPos, fracJourney); 
} 
+0

也許查找Unity的夾法......這可能是你是什麼 – Savlon

+0

後,這不是JavaScript的,這是unityscript – LearnCocos2D

回答

1

我會用Vector3.Distance

function Update() 
{ 

... 

if (Vector3.Distance(target.position, transform.position) > 10) // Change '10' to any distance that you want as a minimum 
{ 
    transform.position = Vector3.Lerp(transform.position, finalPos, fracJourney); 
} 

希望這有助於:)

1

有一個在你的代碼中的小bug,如果更換:

finalPos = target.position + (target.up * localOffset.z); 
finalPos = target.position + (target.forward * localOffset.x); 

有:

finalPos = target.position + (target.up * localOffset.z); 
finalPos += (target.forward * localOffset.x); 

你會得到你想要什麼?