using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[RequireComponent(typeof(UnityEngine.LineRenderer))]
public class DrawCircle : MonoBehaviour
{
    public enum CircleHeight
    {
        Center, Bottom, Top
    };

    public CircleHeight circleheight;
    [Range(0, 50)]
    public int segments = 50;
    [Range(1, 50)]
    public float xradius = 1.5f;
    [Range(1, 50)]
    public float yradius = 1.5f;
    [Range(-10, 10)]
    public float height = 0;
    public bool changeBothRadius = false;
    [Range(0.1f, 2)]
    public float lineThickness = 0.1f;
    public bool minimumRadius = false;
    public bool draw = false;
    public bool animateCircle = false;
    public float animationSpeed = 0.5f;

    private LineRenderer line;
    private Renderer renderer;
    private float Bottom;
    private float Top;
    private float Center = 0;
    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()
    {
        line.startWidth = lineThickness;
        line.endWidth = lineThickness;

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

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

        float angle = 20;

        if (animateCircle == false)
        {
            switch (circleheight)
            {
                case CircleHeight.Center:
                    height = Center;
                    break;
                case CircleHeight.Bottom:
                    height = Bottom;
                    break;
                case CircleHeight.Top:
                    height = Top;
                    break;
            }
        }
        else
        {
            AnimateCircle(Top, Bottom);
        }

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

    private void AnimateCircle(float From, float To)
    {
        if (animStart)
        {
            if (height == Center)
            {
                height = Mathf.Lerp(Center, From, t);
                t += animationSpeed * Time.deltaTime;
                if (height == From)
                {
                    animStart = false;
                }
            }
        }
    }
}


我正在使用animateCircle标志来防止枚举始终将高度重置为Center:

if (animateCircle == false)


然后我添加了一个断点,它没有再传入,而是一直在执行其他部分:

else
            {
                AnimateCircle(Top, Bottom);
            }


而且我在AnimateCircle方法中进行处理后看到高度值为0.6。

但是然后我在行上也添加了一个断点:

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


第一个循环的高度为0.6,但是当循环结束时,当i = 50时,循环又重新开始,我看到高度为0。但是我没有在其他任何地方重设高度。

这是AnimateCircle方法未使用的代码,只是为了显示我尝试过的事情和要执行的操作:

private void AnimateCircle(float From, float To)
    {
        // From = Top To = Bottom
        // height = Center

        if (animStart)
        {
            if (height != From)
            {
                height = Mathf.Lerp(height, From, t);
            }
            else
            {
                height = Mathf.Lerp(From, To, t);
            }
            t += animationSpeed * Time.deltaTime;
            if (height == From || height == To)
                animStart = false;
        }
        else
        {
            height = Mathf.Lerp(From, To, t);
            t += animationSpeed * Time.deltaTime;

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

            if (To == Top)
            {
                height = Mathf.Lerp(Bottom, Top, t);
                t += animationSpeed * Time.deltaTime;

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

            if(To == Bottom)
            {
                height = Mathf.Lerp(Top, Bottom, t);
                t += animationSpeed * Time.deltaTime;

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


例如,游戏在枚举默认状态为“开始”中的“中心”时开始。
如果我拨打AnimateStart(Top, Bottom);AnimateStart(Bottom, Top);

然后在AnimateCircle中,我想根据自己的调用方式将圆从中心移动到顶部或底部。因此,“发自”可以是顶部或底部。

圆圈从中心一次移动到顶部或底部之后,然后在顶部和底部(或底部顶部)之间开始不停的乒乓球。

而且,如果游戏以枚举默认状态(“底部”或“顶部”)开始,则只需在顶部和底部之间启动一个没有中心的乒乓球即可。

编辑:

这是我现在使用的AnimateCircle的代码:

private void AnimateCircle(float From, float To)
    {
        if (animStart)
        {
            // prevent t from exceeding tGoal
            if (t > tGoal)
            {
                t = tGoal;
            }

            // end animation when t reaches goal
            if (t == tGoal)
            {
                animStart = false;
            }

            // advance height according to t
            height = Mathf.Lerp(Bottom, Top, Mathf.PingPong(t, 1f));

            // advance height according to time & speed
            t += animationSpeed * Time.deltaTime;
        }
    }


CreatePoints不变:

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

        float angle = 20;

        if (animateCircle == false)
        {
            switch (circleheight)
            {
                case CircleHeight.Center:
                    // t=0.5f, tGoal=1f to go to top first then stop at top
                    // t=0.5f, tGoal=2f to go to top first then stop at bottom
                    // t=1.5f, tGoal=2f to go to bottom first then stop at bottom
                    // t=1.5f, tGoal=3f to go to bottom first then stop at top

                    t = 0.5f;
                    tGoal = 2f;
                    height = Center;
                    break;
                case CircleHeight.Bottom:
                    t = 0f;
                    tGoal = 1f;
                    height = Bottom;
                    break;
                case CircleHeight.Top:
                    t = 1f;
                    tGoal = 2f;
                    height = Top;
                    break;
            }
        }
        else
        {
            AnimateCircle(Bottom, Top);
        }

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


为了首先移动到顶部,我将AnimateCircle称为:

AnimateCircle(Top, Bottom);


在AnimateCircle中,顶部,底部也像这样:

height = Mathf.Lerp(Top, Bottom, Mathf.PingPong(t, 1f));


在开关案例部分t = 0.5ftGoal = 2f

但是,相反,它先是移动到顶部,然后是快速移动到底部,然后又从底部移动到顶部。

但是,如果我更改t = 1.5ftGoal = 3f;,它将先移至顶部,然后从顶部移至底部,这很好。但是0.5和2不会先移动它。

如果t = 1.5f和tGoal = 2f;它会向上移动到顶部并停在那里。

但是我找不到如何制作“底部”零件,因此它将首先移至“底部”,然后停止或先移至“底部”并移至“顶部”。

最佳答案

添加一个名为tGoal的float字段:

private float tGoal;


根据您希望它开始的位置设置t,并根据您希望它停止动画的位置设置tGoal。如果希望它以Center开头,则必须确定适当的lerp值以开头:

        switch (circleheight)
        {
            case CircleHeight.Center:
                float centerT = Mathf.InverseLerp(Bottom, Top, Center);

                // t=0f+centerT, tGoal=1f to go to top first then stop at top
                // t=0f+centerT, tGoal=2f to go to top first then stop at bottom
                // t=2f-centerT, tGoal=2f to go to bottom first then stop at bottom
                // t=2f-centerT, tGoal=3f to go to bottom first then stop at top

                t = 2f - centerT;
                tGoal = 3f;
                height = Center;
                break;
            case CircleHeight.Bottom:
                t= 0f;
                tGoal = 1f;
                height = Bottom;
                break;
            case CircleHeight.Top:
                t = 1f;
                tGoal = 2f;
                height = Top;
                break;
        }


然后使用Mathf.PingPongt转换为乒乓球的lerp值。当t等于tGoal时停止,确保tGoal永远不会超过它:

private void AnimateCircle()
{
    if (animStart)
    {
        // prevent t from exceeding tGoal
        if (t > tGoal) {
            t = tGoal;
        }

        // end animation when t reaches goal
        if (t == tGoal) {
            animStart = false;
        }

        // advance height according to t
        height = Mathf.Lerp(Bottom, Top, Mathf.PingPong(t,1f));

        // advance height according to time & speed
        t += animationSpeed * Time.deltaTime;

    }
}

关于c# - 为什么可变高度值保持为0?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53859931/

10-11 18:33