2016-10-22 20 views
0

我有一個GameObject,並且在特定時間段內需要縮小。更改特定時間段內GameObject的大小

所以基本上我的GameObject的大小是100x100,我希望它在1秒內縮小到10x10。
現在我可以使用InvokeRepeating但這隻會使它從100x100跳到10x10。
我希望它能夠平穩地從100x100變爲10x10。
還沒有代碼,即時通訊設法弄清楚將如何完成,因爲使用Update不會給我正確的結果。

+1

https://docs.unity3d.com/ScriptReference/Mathf.Lerp.html – 2016-10-22 14:06:08

回答

0

可以用while循環中的CoroutineVector.Lerp完成。這比使用InvokeInvokeRepeating函數更好。

bool isScaling = false; 

IEnumerator scaleOverTime(GameObject objToScale, Vector3 newScale, float duration) 
{ 
    if (isScaling) 
    { 
     yield break; 
    } 
    isScaling = true; 

    Vector3 currentScale = objToScale.transform.localScale; 

    float counter = 0; 
    while (counter < duration) 
    { 
     counter += Time.deltaTime; 
     Vector3 tempVector = Vector3.Lerp(currentScale, newScale, counter/duration); 
     objToScale.transform.localScale = tempVector; 
     yield return null; 
    } 

    isScaling = false; 
} 

使用

public GameObject gameObjectToScale; 
void Start() 
{ 
    StartCoroutine(scaleOverTime(gameObjectToScale, new Vector3(2, 2, 2), 1f)); 
}