2013-09-27 162 views
1

我正在Unity3D中製作2D遊戲(使用Orthello 2D)。在Unity3d中動畫旋轉

自從我的Cocos2D和CoronaSDK切換,我不知道是否有實現的精靈以下行​​爲(或任何Unity3D對象)的方式,因爲它在科羅納的工作:

object = ... 
transition.to (object, { time = 1000, rotation = object.rotation + 100, onComplete = function() 
    // do something 
end }) 

所以精靈在1秒內旋轉100度。附

在我的腳本到一個精靈,我可以在我的Update()功能的旋轉,但它是一個有點不同的方法......

+2

有可供團結幾個插值的實用程序,像的ITween和GoKit。我認爲如果你將它們谷歌他們,你會發現這樣的例子/教程。如果你想編寫自己的插值,Unity對象上還有一堆Lerp/Slerp函數。 http://docs.unity3d.com/Documentation/ScriptReference/30_search.html?q=Lerp – Calvin

+0

謝謝!我想我會先試一試iTween。 – kender

+2

iTween有一個很好的簡單語法,但據說GoKit性能更好,所以如果你正在寫一些將在移動設備上進行大量補間操作的東西,例如,你可能需要在某個時候切換。如果您在Unity資源商店中搜索「tween」,則會看到其他一些解決方案。 – Calvin

回答

0

有這樣做的幾種不同的充方式。例如使用協同程序:

IEnumerator TweenRotation(Transform trans, Quaternion destRot, float speed, float threshold) 
{ 
    float angleDist = Quaternion.Angle(trans.rotation, destRot); 

    while (angleDist > threshold) 
    { 
    trans.rotation = Quaternion.RotateTowards(trans.rotation, destRot, Time.deltaTime * speed); 
    yield return null; 

    float angleDist = Quaternion.Angle(trans.rotation, destRot); 
    } 
} 
0

您可以在Update函數中輕鬆完成。

float timer = 0f; 

void Update() 
{ 
    if(timer <= 1) 
    { 
// Time.deltaTime*100 will make sure we are moving at a constant speed of 100 per second 
     transform.Rotate(0f,0f,Time.deltaTime*100); 
// Increment the timer so we know when to stop 
     timer += Time.deltaTime; 
    } 
} 

如果你需要做另一個100度的旋轉,你只需要重置計時器。

你可以看到不同版本的旋轉功能here的和更多的信息關於救星Time.deltaTime價值here