2016-10-22 3 views
0

私はGameObjectを持っていますが、特定のタイムスパンで縮小する必要があります。特定のタイムスパンでGameObjectのサイズを変更します

私のGameObjectのサイズは基本的に100x100ですから、ちょうど1秒で10x10に縮小します。
今私はInvokeRepeatingを使うことができましたが、それは100x100から10x10にジャンプするだけです。
100x100から10x10にスムーズに移行したいです。
コードはまだありません。Updateを使用すると正しい結果が得られないため、どのように実行されるのか把握しようとしています。

+1

https://docs.unity3d.com/ScriptReference/Mathf.Lerp.html –

答えて

0

whileループでCoroutineVector.Lerpで実行できます。これは、InvokeまたはInvokeRepeating機能を使用するよりも優れています。

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)); 
} 
関連する問題