2016-09-22 66 views
2

我有一個物體A和B.當我點擊物體B時,A旋轉到物體B(A面B)。 B不會面對A.我需要做的是: 當A面向B時,我需要A面對相反的方向。我有一個旋轉的代碼來看看B.如何旋轉後面對相反的方向?旋轉一個物體以面對相反的方向

Vector3 targetDirection = target - transform.position; 
float step = speed * Time.deltaTime; 
Vector3 newDirection = Vector3.RotateTowards (turretDome.transform.forward, targetDirection, step, 0.0F); 
turretDome.transform.rotation = Quaternion.LookRotation (newDirection); 

回答

3

你說的對象A已經面臨物體B,所有你想要做的是逆對象A的後方向?

objectA.transform.rotation = Quaternion.Inverse(objectA.transform.rotation) 

但是讓我們從你的例子假定turretDome爲對象的,那麼你可以這樣做(否定的方向):

turretDome.transform.rotation = Quaternion.LookRotation (-newDirection); 

當然,這兩個片段中不顯示怎麼理順旋轉,因爲您似乎已經知道如何使用time.deltaTime。

只要你不確定,Quaternion.Lerp會幫你做到這一點