2013-03-28 48 views
0

我試圖在兩個基於計時器的對象之間進行一個目標立體掃描器。基於計時器的基於計時器的立體對象

目前,我有以下代碼:

float distCovered = (Time.time - waitTime) * speed; 
    float fracJourney = distCovered/journeyLength; 
    if (_moveDown == false) 
    { 
     if (startTime + waitTime < Time.time) 
     { 

      transform.position = Vector3.Lerp(start.position, end.position, fracJourney); 

      if (transform.position == end.position) 
      { 
       Debug.Log("going down"); 
       _moveDown = true; 

       transform.position = Vector3.Lerp(end.position, start.position, fracJourney); 
      } 


     } 
    } 

    if (_moveDown == true) 
    { 
     float distCovered1 = (Time.time - goDowntimer) * speed; 
     float fracJourney1 = distCovered1/journeyLength; 
     transform.position = Vector3.Lerp(end.position, start.position, fracJourney1); 

     if (transform.position == start.position) 
     { 
      Debug.Log("going up"); 
      // waitTime = 20; 
      _moveDown = false; 

     } 

    } 

此代碼是在我的更新功能,並連接到我的每一個對象,我要上下移動的。每個物體都可以獨立設置等待時間,因此我可以在5秒後移動1次,在10次後移動另一個。

然後,每個目標等待幾秒鐘,然後向下移動。但是,這種運動並不順暢,並且往往會跳起一定的距離。但是,當它回到底部時,它會在_movedown布爾之間發生瘋狂,並且不會移動。

有誰知道我能解決這些問題的方法嗎?

我知道Mathf.PingPong方法不斷地將對象移回兩點之間,但是這不會讓我暫停每個部分的移動。雖然,如果有人知道我可以做到這一點的方式,請讓我知道。可能

transform.position = Vector3.Lerp(start.position, end.position, fracJourney * Time.deltaTime); 

甚至這樣:

+2

請問,對於我們身後的語言障礙,你會解釋一下「lerp」這個詞嗎? – ppeterka

+1

[線性插值](http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Lerp.html) – GalacticCowboy

+0

@GalacticCowboy謝謝,我試過谷歌翻譯第一(沒有結果),並沒有通過代碼讀取在發表評論之前。 Vector3d.Lerp()的API文檔顯示了神祕:)我認爲這是一個我不知道的動詞... – ppeterka

回答

0

試試這個

transform.position = Vector3.Lerp(start.position, end.position, speed * Time.deltaTime); 

X * Time.deltaTime是這樣的情況基本上指示運動方法,通過每秒X米到移動對象。如果沒有deltaTime,它會以每米x米的速度執行這些運動。

+0

您需要總計Time.deltaTime超過一定數量的幀的總時間。正如所寫的,這些功能不起作用。 Lerp是起點和終點之間的總距離。如果Time.deltaTime是恆定的(就像你的幀率完全穩定一樣),那麼Lerp將每幀返回相同的位置。 – Jerdak

0

我在下面添加了一些評論,可能會幫助您澄清您的意圖。

float distCovered = (Time.time - waitTime) * speed; 
float fracJourney = distCovered/journeyLength; 

// Going up... 
if (_moveDown == false) 
{ 
    // Should we be checking this in the other half of the statement too? 
    // Or, outside the conditional altogether? 
    if (startTime + waitTime < Time.time) 
    { 
     transform.position = Vector3.Lerp(start.position, end.position, fracJourney); 

     if (transform.position == end.position) 
     { 
      Debug.Log("going down"); 

      // The way this is structured, we're going to *immediately* fall into the 
      // following block, even if that's not your intended behavior. 
      _moveDown = true; 

      // Going down, but with the fracJourney value as though we were going up? 
      transform.position = Vector3.Lerp(end.position, start.position, fracJourney); 
     } 
    } 
} 

// As noted above, we're going to fall directly into this block on the current pass, 
// since there's no *else* to differentiate them. 
if (_moveDown == true) 
{ 
    // Doesn't follow the same pattern as in the previous block, though that may be intended 
    float distCovered1 = (Time.time - goDowntimer) * speed; 
    float fracJourney1 = distCovered1/journeyLength; 
    transform.position = Vector3.Lerp(end.position, start.position, fracJourney1); 

    if (transform.position == start.position) 
    { 
     Debug.Log("going up"); 
     // waitTime = 20; 
     _moveDown = false; 

     // Should there be a Lerp here, as above, to start heading back the other way again? Or, do you need to remove the other one? 
    } 
} 
1

這裏是對代碼的簡單介紹。它可能會更清潔,但它應該在一個捏。如果/然後阻止w/a'方向'變量,表示我們是從開始到結束還是結束到開始,我已經替換了OP。

Vector3.Lerp()採用在範圍[0,1]一個值,名義上一%距離,從開始到結束點。如果你想扭轉這個方向,你需要做的就是從1中減去,使得範圍變成[1,0](反方向)。這就是我正在做的direction_下面。一旦fracJourney超出範圍,我們切換方向,觸發暫停並重置主定時器。

我把暫停代碼更新()將其從移動代碼中分離出來,但沒有任何理由,代碼塊不能在任FixedUpdate()或更新()。

這個例子是一個在Vector3.Lerp documentation的修改版本:

// additional data members beyond Vector3.Lerp's documentation 
public float PauseTime = 2.0f; 
int direction_ = 1; 
bool doPause_ = false; 

void Update(){ 
    float elapsedTime = Time.time - startTime; 

    // if the elapsed time has exceeded the pause time and we're paused 
    // unpause and reset the startTime; 
    if(elapsedTime > PauseTime && doPause_){ 
     doPause_ = false; 
     startTime = Time.time; 
    } 
} 
void FixedUpdate(){ 
    if(doPause_)return; 

    float distCovered = (Time.time - startTime) * Speed; 
    float fracJourney = distCovered/journeyLength; 

    // +direction means we are going from [0,1], -direction means [1,0] 
    fracJourney = (direction_>0)?fracJourney:1.0f-fracJourney; 
    transform.position = Vector3.Lerp(StartPt.position, EndPt.position, fracJourney); 

    // When fracJourney is not in [0,1], flip direction and pause 
    if(fracJourney > 1.0 || fracJourney < 0.0){ 
     direction_ = -direction_; 
     startTime = Time.time; 
     doPause_ = true; 
    } 
} 

我的「方向」成員可以很容易地是一個布爾值,但我喜歡有其他目的的簽署方向。