【发布时间】:2020-01-02 20:31:01
【问题描述】:
基本上,此代码将游戏对象的大小从大到小,然后从小到大..(重复)
现在,如果您希望通过在游戏运行时将 bool 可重复设置为 true 或 false 来使其变小变大或变大变小,您会怎么做。
就像,在更新函数中,我们有 if 函数。我希望在鼠标按下时发生yield return RepeatLerp(minScale, maxScale, duration);,在鼠标启动时发生yield return RepeatLerp(tramsform.localscale // (current size of object), minScale, duration);。
public class TouchMechanic : MonoBehaviour {
Vector3 minScale;
public Vector3 maxScale;
public bool repeatable;
public float speed = 2f;
public float duration = 5f;
public float timeStartedlerping ;
public float z;
IEnumerator Start()
{
minScale = transform.localScale;
while (repeatable)
{
yield return RepeatLerp(minScale, maxScale, duration);
yield return RepeatLerp(maxScale, minScale, duration);
}
}
public IEnumerator RepeatLerp(Vector3 a, Vector3 b, float time)
{
float i = 0.0f;
float rate = (1.0f / time) * speed;
while (i < 1.0f)
{
i += Time.deltaTime * rate;
transform.localScale = Vector3.Lerp(a, b, i);
yield return null;
}
}
}
【问题讨论】: