您可以使用Vector3.Lerp 在两点之间生成一个点。将0.5 传递给它的t 参数将使它在PositionA 和PositionB 之间的middle 位置。
要在两点之间生成多个点,您只需在循环中使用Vector3.Lerp。
这是一个执行此操作的函数:
void generatePoints(Vector3 from, Vector3 to, Vector3[] result, int chunkAmount)
{
//divider must be between 0 and 1
float divider = 1f / chunkAmount;
float linear = 0f;
if (chunkAmount == 0)
{
Debug.LogError("chunkAmount Distance must be > 0 instead of " + chunkAmount);
return;
}
if (chunkAmount == 1)
{
result[0] = Vector3.Lerp(from, to, 0.5f); //Return half/middle point
return;
}
for (int i = 0; i < chunkAmount; i++)
{
if (i == 0)
{
linear = divider / 2;
}
else
{
linear += divider; //Add the divider to it to get the next distance
}
// Debug.Log("Loop " + i + ", is " + linear);
result[i] = Vector3.Lerp(from, to, linear);
}
}
用法:
//The two positions to generate point between
Vector3 positionA = new Vector3(0, 0, 0);
Vector3 positionB = new Vector3(254, 210, 50);
//The number of points to generate
const int pointsCount = 10;
//Where to store those number of points
private Vector3[] pointsResult;
void Start()
{
pointsResult = new Vector3[pointsCount];
generatePoints(positionA, positionB, pointsResult, pointsCount);
}
10 个不同的数组点现在存储在pointsResult 变量中。