2015-11-04 39 views
2

我有一個2d對撞機的立方體gameObject,當有人撞上相機應該是10個單位,但順利,相機去正確的位置,但沒有阻尼。Vector3.Lerp不在我的相機上工作

public Transform target; 
public float damping = 0.1f; 

void OnTriggerEnter2D(Collider2D other) 
{ 
    Vector3 newPoint = Vector3.Lerp(target.transform.position, 
            target.transform.position + Vector3.up * 10.0f, 
            damping); 

    target.transform.position = newPoint; 
} 
+0

我可以想象你需要完成的'other'移動動畫,然後。嘗試爲此啓動協程,並確保在相同的碰撞情況下不再啓動它。 – SimpleVar

回答

4

這是不工作的原因是因爲你沒有正確使用Lerp。線性插值(Lerp)的數學運算如下:

假設我們有一個點(0, 0)和一個點(1, 1)。爲了計算兩點之間的差距,我們提供的值爲0.0f1.0f。這個t表示兩點之間的比率(0.0f(0, 0)1.0f(1, 1))。例如,值0.5f將導致點​​。

現在用一個不那麼簡單的例子展開這個,考慮兩點: var a = Vector2(1.0f, -1.0f)var b = Vector2(0.0f, 1.0f)Lerp(a, b, 0.5f)的結果是Vector2(0.5f, 0.0f),因爲這是中途點。我們打電話給回答c。給我們答案的公式是c = a + (0.5f * (b - a))。這來自對線性代數的基本理解,其中從彼此中減去兩個點導致向量,並且將向量添加到點給出另一點。

好吧,現在就讓這段代碼按照你想要的方式工作。你的腳本可能看起來像這樣:

float moveTime = 10.0f; // In seconds 
float moveTimer = 0.0f; // Used for keeping track of time 

bool moving = false; // Flag letting us know if we're moving 

float heightChange = 10.0f; // This is the delta 

// These will be assigned when a collision occurs 
Vector3 target; // our target position 
Vector3 startPos; // our starting position 

void OnTriggerEnter2D(Collider2D other) 
{ 
    if (!moving) 
    { 
     // We set the target to be ten units above our current position 
     target = transform.position + Vector3.up * heightChange; 

     // And save our start position (because our actual position will be changing) 
     startPos = transform.position; 

     // Set the flag so that the movement starts 
     moving = true; 
    } 
} 

void Update() 
{ 
    // If we're currently moving and the movement hasn't finished 
    if (moving && moveTimer < moveTime) 
    { 
     // Accumulate the frame time, making the timer tick up 
     moveTimer += Time.deltaTime; 


     // calculate our ratio ("t") 
     float t = moveTimer/moveTime; 

     transform.position = Vector3.Lerp(startPos, target, t); 
    } 
    else 
    { 
     // We either haven't started moving, or have finished moving 
    } 
} 

希望這有助於!

+0

Jordan Ellis這個真正做的工作,你解釋得很好,簡單直接。有一個關於這個問題。好吧,我需要把腳本放在我的相機中,那麼我怎麼能讓這個triger成爲其他對象的觸發器。 例如:我有1盒(triger),我的玩家正在攀爬,當他觸發這個觸發器時,我的相機在那裏平穩地移動,因此他可以看到更多的地圖。 (對不起,我的英文= P) –

+0

找到了一種方式,感謝您的幫助! –

+0

你能用你的代碼來幫忙嗎?我改變了主意,我想要的基本上是這樣的:一個玩家跳到我在MainCamera中製作的BoxCollider2D時,這個代碼發生了,但它只能工作一次,因此我們需要設置move = false;再次,我不知道我怎麼能把我嘗試不同的方式,什麼都沒有。 –

0

i在觸發器上設置腳本,然後添加遊戲對象凸輪,然後將相機拖動到腳本,並添加凸輪。在轉換..我們的幫助! =)

公共類CameraUp:MonoBehaviour {

public GameObject cam; 

float moveTime = 10.0f; // In seconds 
float moveTimer = 0.0f; // Used for keeping track of time 

bool moving = false; // Flag letting us know if we're moving 

float heightChange = 10.0f; // This is the delta 

// These will be assigned when a collision occurs 
Vector3 target; // our target position 
Vector3 startPos; // our starting position 


void Start() 
{ 


} 


void Update() 
{ 

    // If we're currently moving and the movement hasn't finished 
    if (moving && moveTimer < moveTime) 
    { 
     // Accumulate the frame time, making the timer tick up 
     moveTimer += Time.deltaTime; 


     // calculate our ratio ("t") 
     float t = moveTimer/moveTime; 

     cam.transform.position = Vector3.Lerp(startPos, target, t); 
    } 
    else 
    { 
     // We either haven't started moving, or have finished moving 
    } 

} 

void OnTriggerEnter2D(Collider2D other) 
{ 
    if (!moving) 
    { 
     // We set the target to be ten units above our current position 
     target = transform.position + Vector3.up * heightChange; 

     // And save our start position (because our actual position will be changing) 
     startPos = cam.transform.position; 

     // Set the flag so that the movement starts 
     moving = true; 
    } 
} 

}