我已经能够使用以下协程制作一个从一端到另一端的动画:
IEnumerator AnimateArc(float duration)
{
float waitDur = duration / pts;
for (int i = 0; i <= pts; i++)
{
float x = center.x + radius * Mathf.Cos(ang * Mathf.Deg2Rad);
float y = center.y + radius * Mathf.Sin(ang * Mathf.Deg2Rad);
arcLineRend.positionCount = i + 1;
arcLineRend.SetPosition(i, new Vector2(x, y));
ang += (float)totalAngle / pts;
yield return new WaitForSeconds(waitDur);
}
}
如何从中间向两个方向设置此弧的动画
>以恒定速度
>轻松(动画在中途(半部分)稍微快一点,然后减速到最后)
最佳答案 那么,对于双向部分,您可能需要使用两个线渲染器并将第二个方向的坐标更改为center.x – radius * Mathf.Cos((180f-ang)* Mathf.Deg2Rad)和y分别.
恒速是你现在拥有的,对于另一个,搜索slerp(球形插值.使用一些bool在这两者之间切换.
另外,为了给自己试一试,我已经在项目中添加了代码,我必须更改一些内容才能使其正常工作:
[RequireComponent(typeof(LineRenderer))]
public class LineCalculator : MonoBehaviour
{
public float pts = 15f;
public Vector2 center = new Vector2(0, 0);
public float radius = 5f;
public float totalAngle = 180f;
LineRenderer lR;
private void Awake()
{
lR = GetComponent<LineRenderer>();
}
private void Start()
{
StartCoroutine(AnimateArc(5f));
}
IEnumerator AnimateArc(float duration)
{
float waitDur = duration / pts;
WaitForSeconds sleep = new WaitForSeconds(waitDur); // This is just for saving a tiny bit of performance
float ang = 0f;
float step = totalAngle / (pts-1); // -1 because I ended up with one point to much (pts seems to not include the starting point)
for (int i = 0; i < pts; i++) // Only go to i < pts
{
float x = center.x + radius * Mathf.Cos(ang * Mathf.Deg2Rad);
float y = center.y + radius * Mathf.Sin(ang * Mathf.Deg2Rad);
lR.positionCount = i + 1;
lR.SetPosition(i, new Vector2(x, y));
ang += step;
yield return sleep;
}
}
}