首页 > 解决方案 > 如何在 3 个高度状态之间不停地改变绘图圆的高度自动平滑?

问题描述

前几名:

public enum CircleHeight
    {
        Center, Bottom, Top
    };

    public CircleHeight circleheight;
    public bool draw = false;

在开始:

void Start()
    {
        circleheight = CircleHeight.Center;
    }

在更新中:

void Update()
    {
        line.startWidth = lineThickness;
        line.endWidth = lineThickness;

        if (draw)
        {
            line.enabled = true;
            CreatePoints();
        }
        else
        {
            line.enabled = false;
        }
    }

在 CreatePoints 内部:

void CreatePoints()
    {
        float x;
        float z;

        float angle = 20;

        switch (circleheight)
        {
            case CircleHeight.Center:
                height = 0;
                break;
            case CircleHeight.Bottom:
                height = transform.InverseTransformPoint(renderer.bounds.min).y + 0.1f;
                break;
            case CircleHeight.Top:
                height = transform.InverseTransformPoint(renderer.bounds.max).y;
                break;
        }

        for (int i = 0; i < (segments + 1); i++)
        {
            x = Mathf.Sin(Mathf.Deg2Rad * angle) * xradius;
            z = Mathf.Cos(Mathf.Deg2Rad * angle) * yradius;

            line.SetPosition(i, new Vector3(x, height, z));

            angle += (360f / segments + 1);
        }
    }

在 CreatePoints 方法中,我可以更改高度的枚举状态。但是现在我想添加一个标志,如果它在 CreatePoints 内为真,它将自动缓慢平滑地改变高度状态。

我尝试在 CreatePoints 中添加这部分:

if(animateCircle)
        {
            t += Time.deltaTime / 3;
            height = Mathf.Lerp(0, 1, t);
            height = Mathf.Lerp(1, transform.InverseTransformPoint(renderer.bounds.min).y + 0.1f, t);
        }

它确实从上到下缓慢移动,但只有一次。它在所有 3 个高度状态之间移动,而不是自动重复。所以它还没有像我想要的那样运作良好。

标签: c#unity3d

解决方案


一个可行的解决方案:

在顶部:

public enum CircleHeight
    {
        Center, Bottom, Top
    };

public CircleHeight circleheight;
[Range(0, 50)]
public int segments = 50;
private LineRenderer line;
private Renderer renderer;
private float Bottom;
private float Top;
private float t = 0f;

在开始时:

void Start()
    {
        circleheight = CircleHeight.Center;

        line = gameObject.GetComponent<UnityEngine.LineRenderer>();
        line.positionCount = segments + 1;
        line.useWorldSpace = false;
        renderer = gameObject.GetComponent<Renderer>();

        Bottom = transform.InverseTransformPoint(renderer.bounds.min).y + 0.1f;
        Top = transform.InverseTransformPoint(renderer.bounds.max).y + 0.1f;
    }

更新中:

void Update()
    {
      CreatePoints();
    }

在创建点中:

bool animStart = false;
void CreatePoints()
{
    float x;
    float z;

    float angle = 20;

    switch (circleheight)
    {
        case CircleHeight.Center:
            height = 0;
            break;
        case CircleHeight.Bottom:
            height = Bottom;
            break;
        case CircleHeight.Top:
            height = Top;
            break;
    }

    if (animateCircle)
    {
        if (animStart == false)
        {
            height = Mathf.Lerp(0, Top, t);
            t += animationSpeed * Time.deltaTime;
            if (height == Top)
                animStart = true;
        }
        else
        {
            height = Mathf.Lerp(Bottom, Top, t);
            t += animationSpeed * Time.deltaTime;

            if (t > 1.0f)
            {
                float temp = Top;
                Top = Bottom;
                Bottom = temp;
                t = 0.0f;
            }
        }
    }

    for (int i = 0; i < (segments + 1); i++)
    {
        x = Mathf.Sin(Mathf.Deg2Rad * angle) * xradius;
        z = Mathf.Cos(Mathf.Deg2Rad * angle) * yradius;

        line.SetPosition(i, new Vector3(x, height, z));

        angle += (360f / segments + 1);
    }
}

由于高度从中心高度开始 = 0,我添加了另一个标志 animStart 以第一次将圆移动到顶部,然后开始在顶部和底部之间移动。

现在绘制的圆在顶部和底部平滑之间上下平滑移动。


推荐阅读