Unity中如何同步Circle与物体高度?解决LineRenderer延迟偏移问题
LineRenderer高度与Cube位置不同步问题
调整绘制线条的高度时,若同时调整Cube等物体的位置,LineRenderer线条会出现延迟,无法与Cube同步移动高度,导致Cube上下移动速度更快,与绘制线条产生明显高度偏移。
代码相互关联无法精简,但复制粘贴可正常运行。
截图情况
- 绘制Circle高度为0时,Cube位于Circle上,状态正常:

- 在Inspector中调整高度后,Cube上升速度远快于Circle:

相关代码
CreatePoints方法(设置Circle高度)
void CreatePoints() { float angleStep = 360f / segments; float angle = 0f; float y = height; // Fixed height for the circle plane // Adjust radii based on parent's scale Vector3 parentScale = transform.parent ? transform.parent.lossyScale : Vector3.one; float adjustedXRadius = xradius / parentScale.x; float adjustedYRadius = yradius / parentScale.z; for (int i = 0; i <= segments; i++) // <= to close the circle { float x = Mathf.Sin(Mathf.Deg2Rad * angle) * adjustedXRadius; float z = Mathf.Cos(Mathf.Deg2Rad * angle) * adjustedYRadius; line.SetPosition(i, new Vector3(x, y, z)); angle += angleStep; } }
AdjustPositions方法(调整Cube位置)
private void AdjustPositions(int num, Vector3 point, float xRadius, float yRadius) { if (objects == null || objects.Length == 0 || num <= 0) return; for (int i = 0; i < objects.Length; i++) { if (objects[i] == null) continue; // Skip if the object was destroyed // Distance around the ellipse var radians = 2 * Mathf.PI / num * i; // Calculate the new position on the ellipse var x = Mathf.Cos(radians) * xRadius; var z = Mathf.Sin(radians) * yRadius; var spawnPos = point + new Vector3(x, height, z); // Set position on ellipse // Update object position objects[i].transform.position = spawnPos; // Rotate the object to face the center objects[i].transform.LookAt(point); } }
已尝试的解决方案
- 将LineRenderer的
useWorldSpace设为true,效果不佳 - 用
LateUpdate替代Update,改善不明显 - 尝试自动计算偏移量并添加到Circle上,未成功,不清楚如何正确计算所需偏移量
内容的提问来源于stack exchange,提问作者Daniel Lip
相关产品推荐
相关产品推荐

