whileループでCoroutine
とVector.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));
}
https://docs.unity3d.com/ScriptReference/Mathf.Lerp.html –