Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用多个位置设置图形线条渲染器的动画_C#_Unity3d - Fatal编程技术网

C# 使用多个位置设置图形线条渲染器的动画

C# 使用多个位置设置图形线条渲染器的动画,c#,unity3d,C#,Unity3d,我有一个LineRenderer,有很多位置,我正在尝试从点a->b、b->c等处设置线条动画 首先,我保存行的所有位置,然后重置positionCount,使其在开始时不可见。但是,当我在循环中绘制lineRenderer时,在每次迭代中增加positionCount,当开始绘制下一行时,前一行会稍微抖动,宽度会瞬间改变 以下是代码: public float LineDrawSpeed; void Start() { lineRenderer = GetComponent<Lin

我有一个
LineRenderer
,有很多位置,我正在尝试从点a->b、b->c等处设置线条动画

首先,我保存行的所有位置,然后重置
positionCount
,使其在开始时不可见。但是,当我在循环中绘制
lineRenderer
时,在每次迭代中增加
positionCount
,当开始绘制下一行时,前一行会稍微抖动,宽度会瞬间改变

以下是代码:

public float LineDrawSpeed;

void Start()
{
  lineRenderer = GetComponent<LineRenderer>();
  int lineCountInLineRenderer = lineRenderer.positionCount - 1;
  var startPositions = new Vector3[lineRenderer.positionCount];
  lineRenderer.GetPositions(startPositions);
  lineRenderer.positionCount = 1;
  StartCoroutine(LineDrawCoroutine(startPositions));
}
公共浮点数换行速度;
void Start()
{
lineRenderer=GetComponent();
int lineCountInLineRenderer=lineRenderer.positionCount-1;
var startPositions=newvector3[lineRenderer.positionCount];
lineRenderer.GetPositions(起始位置);
lineRenderer.positionCount=1;
Start例程(LineDrawCoroutine(startPositions));
}
以下是协同程序:

IEnumerator LineDrawCoroutine(Vector3[] positions)
{
  for (int i = 0; i < positions.Length - 1; i++)
  {
    if (lineRenderer.positionCount <= i + 1)
    lineRenderer.positionCount++;

    lineRenderer.SetPosition(i, positions[i]);
    float timePass = 0f;
    while (timePass < LineDrawSpeed)
    {
      var factor = timePass / LineDrawSpeed;
      factor = Mathf.SmoothStep(0, 1, factor);

      lineRenderer.SetPosition(i + 1, Vector3.Lerp(positions[i], positions[i + 1], factor));

      timePass += Mathf.Min(Time.deltaTime, LineDrawSpeed - timePass);
      yield return null;
    }
  }
}
IEnumerator LineDrawCoroutine(矢量3[]位置)
{
对于(int i=0;iif(lineRenderer.positionCount我发现这个宽度变化主题非常有趣。
据我所知,有两点需要考虑

1.-线条渲染器渲染广告牌线条

广告牌是三维世界中的二维元素,其方向会自动计算,使其始终面向摄影机。这解释了宽度随摄影机移动而变化的原因

2.-如果相机未移动,请考虑:如文档中所定义,“宽度”属性定义:“宽度值和曲线值,用于控制轨迹沿其长度的宽度。 曲线在每个顶点处采样,因此其精度受轨迹中顶点数的限制。轨迹的总宽度由“宽度”值控制。“

因此,当您在动态地更改直线的顶点时,轨迹的总宽度可能会发生变化


因此,我认为您的算法运行良好,并且线条渲染器组件附带了宽度变化。

我非常确信您的问题在于您正在增加位置计数 一次又一次,但不是100%确定

不管怎样,下面是如何增量增加行长度的工作代码。它只是一个
IEnumerator
,但您可以在
start例程中调用它,它应该可以工作

这将适用于直线,但需要对曲线进行一些调整(但我认为如果您愿意,您可以使其工作)

为了进一步解释这一点,我没有增加和增加
positionCount
,然后添加向量,而是简单地将positionCount设置为所需的向量量,然后用所需的位置递增地填充每个向量。当设置它们时,它们默认为
0,0,0
,所以确保这不会给你带来任何问题

下面是我使用的代码:

//  Using IEnumerator to ensure that the line is drawn over a specific amount of time
private IEnumerator DrawLine()
{
    float renderTime = 2f;                                      // total time for full line render
    int renderSteps = 75;                                       // desired resolution of render (higher amt of steps creates smoother render)
    float timeBetweenSteps = renderTime / renderSteps;          // time between each render step

    //  Grab the LineRenderer component that is attached to the gameObject and defined in Inspector
    LineRenderer lineRenderer = yourGameObject.GetComponent<LineRenderer>();

    //  Declare the endpoint
    Vector3 endPoint = new Vector3(0, 5, 0);

    //  Take the end point and break into the amount of steps we need in order to create 
    //  fully rendered line
    //  Create an additiveVector for the forLoop that will step through the renderSteps
    //  >>  Start at zero becuase zero is the localOrigin
    Vector3 stepVector = endPoint / renderSteps;
    Vector3 additiveVector = Vector3.zero;

    //  Setting the line's position element count to the render resolution because we will
    //  have to step through 
    lineRenderer.positionCount = renderSteps;

    //  For Loop that steps through each position element in the Line
    for (int i = 0; i != lineRenderer.positionCount; i++)
    {
        lineRenderer.SetPosition(i, additiveVector);            // set the position element to the additiveVEctor
        additiveVector += stepVector;                           // add one step to the additiveVector
        yield return new WaitForSeconds(timeBetweenSteps);      // Wait the appropriate step time before repeating the loop
    }
}
//使用IEnumerator确保在特定时间内绘制线
专用IEnumerator抽绳()
{
float renderTime=2f;//整行渲染的总时间
int renderSteps=75;//所需的渲染分辨率(步长越大,渲染越平滑)
float timeBetweenSteps=renderTime/renderSteps;//每个渲染步骤之间的时间
//抓取附加到游戏对象并在Inspector中定义的LineRenderer组件
LineRenderer LineRenderer=yourGameObject.GetComponent();
//声明端点
Vector3端点=新的Vector3(0,5,0);
//以结束点为起点,深入到我们需要的步骤中,以创建
//完全渲染线
//为forLoop创建一个additiveVector,它将逐步执行渲染步骤
//>>从零开始,因为零是本地原点
Vector3步骤向量=端点/渲染步骤;
Vector3加法器=Vector3.0;
//将线的位置元素计数设置为渲染分辨率,因为我们将
//我必须通过
lineRenderer.positionCount=渲染步骤;
//For循环,该循环遍历行中的每个位置元素
对于(int i=0;i!=lineRenderer.positionCount;i++)
{
lineRenderer.SetPosition(i,additiveVector);//将position元素设置为additiveVector
additiveVector+=stepVector;//向additiveVector添加一个步骤
yield return new WaitForSeconds(timeBetweenSteps);//在重复循环之前,请等待适当的步骤时间
}
}

如果你有任何问题,请告诉我。我希望这有帮助!

你能澄清这个问题吗?这是你的主要问题:我如何使这个动画不发生任何抖动或宽度变化?@什么是的,没错。如何使LR具有多个位置且不发生奇怪的线条视觉变化。@什么是,直线。明白了。我在下面添加了一个解决方案。让m我知道这是否适用于你!如果我理解正确,那么是的。这基本上就是我的代码所做的,所以我认为你应该是好的。我在开始时设置了positionCount,然后只设置其中的向量,而不是增加实际的positionCount值。谢谢你的解释。所以,也许可以用你来绘制LineRenderert改变位置计数。我会朝这个方向看。我不想在每个A-B线上创建单独的LR。我想在一个LR中创建绘制线动画。我没有想到。你可以用一个脚本处理多个LR并循环它们。它们都有相同的宽度,你可以解决最小化宽度问题…你的意思是在每个简单的LR上创建行一个单独的LR?是的,我的意思是,对于你用LR创建一个游戏对象的每个部分,你创建一个全局控制器对象,并对每个对象进行引用,这样你就可以根据