如何在两个方向上从中间设置圆弧动画

时间:2018-04-21 14:19:27

标签: unity3d

我已经能够使用以下协程制作一个从一端到另一端的动画弧:

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);

}
}

如何从中间向两个方向设置此弧的动画

  1. 以恒定速度
  2. 轻松(动画在中途(半部分)稍微快一点,然后减速到最后)

1 个答案:

答案 0 :(得分:1)

嗯,对于双向部分,您可能需要使用两个线渲染器,并将第二个方向的坐标分别更改为center.x - radius * Mathf.Cos((180f-ang) * Mathf.Deg2Rad)和y。

恒速是你现在拥有的,另一个,搜索slerp (spherical interpolation。使用一些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;
        }
    }
}