为什么可变高度值保持为0?

时间:2018-12-19 22:18:56

标签: c# unity3d

SELECT [Unique name #] = DENSE_RANK() OVER(ORDER BY c.name)
    , [Column] = c.name 
    , [Column type] = t.name
    , [Column max length] = c.max_length
    , [Column precision] = c.precision
    , [Table schema] = OBJECT_SCHEMA_NAME(c.object_id)
    , [Table name] = OBJECT_NAME(c.object_id)
FROM sys.columns as c
INNER JOIN sys.systypes as t ON t.type = c.system_type_id
WHERE OBJECTPROPERTY(c.object_id,'IsTable') = 1
    and  OBJECT_SCHEMA_NAME(c.object_id) != 'sys'
ORDER BY c.name;

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

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

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

if (animateCircle == false)

在执行AnimateCircle方法中的处理后,我看到高度值为0.6。

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

else
            {
                AnimateCircle(Top, Bottom);
            }

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

这是AnimateCircle方法未使用的代码,只是为了展示我尝试过的方法和意图:

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

例如,游戏在枚举默认状态为“开始”中的“中心”时开始。 如果我打电话给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);

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

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

如果游戏以枚举默认状态(底部或顶部)开始,则只需在顶部和底部之间启动乒乓球即可,而无需居中。

编辑:

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

AnimateStart(Bottom, Top);

CreatePoints不变:

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

首先移动到顶部,我这样称呼AnimateCircle:

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

在开关案例height = Mathf.Lerp(Top, Bottom, Mathf.PingPong(t, 1f)); t = 0.5f

但是首先移到顶部,然后快速从底部移到顶部。

但是如果我更改tGoal = 2ft = 1.5f,它将首先移动到顶部,然后从顶部移动到底部,这很好。但是0.5和2不会先将其向上移动。

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

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

1 个答案:

答案 0 :(得分:3)

添加一个名为tGoal的浮动字段:

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;

    }
}