我在谷歌上搜索,发现linerenderer高度位置有问题。当更改绘制线的高度位置时,如果我也更改对象的位置,如立方体,则线渲染器线将有一些延迟,并且不会与立方体移动相同的高度。导致立方体更快地上下移动,并且与绘制的线有很大的偏移高度。
不确定如何减少代码,因为它们都是相互关联的。但代码正在使用复制/粘贴。
这是绘制的圆圈高度为0时的屏幕截图。立方体在圆上。
这很好。
现在是在检查器中稍微改变高度时的屏幕截图。
立方体向上移动得很快,而圆圈移动得慢得多。
在CreatePoint方法中,我将y变量设置为高度变量,然后将其设置为圆。这会改变圆的高度。
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中,i根据高度更改立方体在y轴上的位置。
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);
}
}
我试图在代码中将useworldspace设置为true,我将其设置为false,但即使为true,它也不能很好地工作。我试着用lateupdate代替update,但它也没有太大变化。我试图计算自动偏移量并将偏移量添加到圆上,但也不起作用,我想我真的不知道如何自动计算所需的偏移量。