随着时间的推移缩放GameObject

我制作了一个统一的测试游戏,这样当我点击一个按钮时,它会产生一个从工厂类创建的圆柱体。 我正在尝试这样做,当我创建圆柱体时,它的高度在接下来的20秒内缩小。 我发现的一些方法很难转化为我正在做的事情。 如果你能引导我走向正确的方向,我将非常感激。

这是我的圆柱类代码

public class Cylinder : Shape { public Cylinder() { GameObject cylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder); cylinder.transform.position = new Vector3(3, 0, 0); cylinder.transform.localScale = new Vector3(1.0f, Random.Range(1, 2)-1*Time.deltaTime, 1.0f); cylinder.GetComponent().material.color = Random.ColorHSV(); Destroy(cylinder, 30.0f); } } 

这可以使用协程函数中的Time.deltaTimeVector3.Lerp来完成。 类似于随时间旋转GameObject和随时间 移动GameObject问题。 修改它有点做这个。

 bool isScaling = false; IEnumerator scaleOverTime(Transform objectToScale, Vector3 toScale, float duration) { //Make sure there is only one instance of this function running if (isScaling) { yield break; ///exit if this is still running } isScaling = true; float counter = 0; //Get the current scale of the object to be moved Vector3 startScaleSize = objectToScale.localScale; while (counter < duration) { counter += Time.deltaTime; objectToScale.localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration); yield return null; } isScaling = false; } 

用法

将在20秒内扩展GameObject:

 StartCoroutine(scaleOverTime(cylinder.transform, new Vector3(0, 0, 90), 20f)); 

看看Lerp 。 如何使用它的一般示例将是这样的:

 float t = 0; Update() { t += Time.deltaTime; cylinder.localScale = new Vector3(1, Mathf.Lerp(2f, 1f, t/3f), 1); // shrink from 2 to 1 over 3 seconds; } 

您将创建一个新的monobehaviour脚本并将其添加到基元。 然后你将使用monobehaviour(或使用coroutine)的“更新”方法随时间变化对象。

Monobehaviour必须如下所示:

 public class ShrinkBehaviour : MonoBehaviour { bool isNeedToShrink; Config currentConfig; float startTime; float totalDistance; public void StartShrink(Config config) { startTime = Time.time; currentConfig = config; totalDistance = Vector3.Distance(currentConfig.startSize, currentConfig.destinationSize); isNeedToShrink = true; transform.localScale = config.startSize; } private void Update() { if (isNeedToShrink) { var nextSize = GetNextSize(currentConfig); if (Vector3.Distance(nextSize, currentConfig.destinationSize) <= 0.05f) { isNeedToShrink = false; return; } transform.localScale = nextSize; } } Vector3 GetNextSize(Config config) { float timeCovered = (Time.time - startTime) / config.duration; var result = Vector3.Lerp(config.startSize, config.destinationSize, timeCovered); return result; } public struct Config { public float duration; public Vector3 startSize; public Vector3 destinationSize; } } 

要使用此function,您必须执行下一步:

 public Cylinder() { GameObject cylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder); var shrink = cylinder.AddComponent(); shrink.StartShrink(new ShrinkBehaviour.Config() { startSize = Vector3.one * 10, destinationSize = Vector3.one * 1, duration = 20f }); cylinder.transform.position = new Vector3(3, 0, 0); cylinder.GetComponent().material.color = Random.ColorHSV(); Destroy(cylinder, 30.0f); } 

你必须记住,monobehaviour-script必须在单独的文件中,并且必须具有类似于monobehaviour-class名称的名称。 例如,ShrinkBehaviour.cs;