我有一个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));
}
这是协程:
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;
}
}
}
机制很好,但是动画有些问题。
答案 0 :(得分:2)
我发现这个宽度变化主题非常有趣。 据我检查,有两点要考虑。
1。-LineRenderer渲染广告牌线。
https://docs.huihoo.com/unity/5.3/Documentation/en/Manual/class-LineRenderer.html
广告牌是嵌入3D世界中的2D元素,其方向会自动计算出来,使其始终面向相机。这说明了宽度随相机移动的变化。
2.-如果摄像机没有移动,请考虑以下因素:根据文档中的定义,width属性定义:“宽度值和曲线值,用于控制沿其长度的轨迹宽度。 曲线是在每个顶点处采样的,因此其精度受路径中顶点数的限制。路径的总宽度由宽度值控制。“
https://docs.unity3d.com/Manual/class-LineRenderer.html
因此,当您要动态更改线的顶点时,路径的整体宽度可能会发生变化。
因此,我认为您的算法工作正常,并且宽度变化随线渲染器组件一起出现。
答案 1 :(得分:1)
我非常有信心您的问题在于您正在增加职位数量 一遍又一遍地。不过不是100%肯定。
无论如何,下面是有关如何逐步增加行长的工作代码。它只是IEnumerator
,但您可以在StartCoroutine
中调用它,它应该可以工作
这将适用于直线,但将对曲线进行一些调整(但我想,如果您愿意的话,可以使其起作用)。
为对此进行更多说明,而不是增加和增加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
}
}
如果您有任何疑问,请告诉我。我希望这会有所帮助!