我有一个开始和结束,我需要从开始到结束在线上放置可调节数量的光线。但我无法弄清楚如何正确放置它们。我快完成了,但是光线没有放在最后。
绿色 (2, 0, 2) 开始,红色 (2, 0, -2) 结束,4
之间的距离这是我的 2 条光线的结果,中心光线需要在末端(红色光线),下一条光线添加应该在开始和结束光线的中心
如果我添加第三条射线会发生什么,结束射线位置未使用
还有5条射线。第一条和最后一条光线需要位于起点和终点。其他3条射线应该在
之间这是我的代码:
public class VehicleAroundCast : MonoBehaviour
{
[SerializeField] private int sideRaysAmount;
[SerializeField] private Vector3 offset;
private void Update()
{
Vector3 startRayPos = transform.position + Vector3.left * offset.x + Vector3.forward * offset.z;
Vector3 endRayPos = transform.position + Vector3.left * offset.x + Vector3.back * offset.z;
float dist = Vector3.Distance(startRayPos, endRayPos);
Debug.DrawRay(startRayPos, Vector3.down, Color.green);
Debug.DrawRay(endRayPos, Vector3.down, Color.red);
for (int i = 0; i < sideRaysAmount; i++)
{
float step = dist / sideRaysAmount * i;
Vector3 position = (transform.position + Vector3.left * offset.x + Vector3.forward * offset.z) + Vector3.back * step;
Debug.DrawRay(position, Vector3.down);
}
}
}
在通过评论进行澄清之后,我会做一些不同的事情。我将编辑您的代码,使其按您的需要工作,并在代码注释中进行解释。
public class VehicleAroundCast : MonoBehaviour
{
[SerializeField] [Min(0)] private int intermediateRayCount;
// i renamed this field so the name is more descriptive, intermediate meaning between start and end
// to make sure that no number below 0 can be assigned i added the [Min(0)] attribute, a number
// below 0 would break the logic and wouldn't make sense
[SerializeField] private Vector3 offset;
private void Update ()
{
// we dont want to clog up our Update() method with tons of lines of code
// this serves better maintainability as well
CastRays();
}
private void CastRays ()
{
Vector3 startRayPos = transform.position + Vector3.left * offset.x + Vector3.forward * offset.z;
Vector3 endRayPos = transform.position + Vector3.left * offset.x + Vector3.back * offset.z;
// if we dont have any intermediate rays we can save on the calculation that is necessary when we
// have intermediates, we just cast the the two rays and end the method by returning
if (intermediateRayCount == 0)
{
Debug.DrawRay(startRayPos, Vector3.down, Color.green);
Debug.DrawRay(endRayPos, Vector3.down, Color.red);
return;
}
// instead of using Vector3.Distance() we'll calculate the directional vector manually this way
// we can get not only the distance (directional vectors magnitude) but the direction to move
// along to space the rays as well
// subtarting the start position from the end position gives us the directional vector
Vector3 direction = endRayPos - startRayPos;
// a directional vectors magnitude gives the distance from start to end
float distance = direction.magnitude;
// after the distnace has been established we normalize the direction so that it's length is 1
direction.Normalize();
// we have at least two rays (start and end) so our total is the rays between start and end plus two
int totalRayCount = intermediateRayCount + 2;
// the space between the individual rays, we have to subtract one from the totalRayCount in order to
// place the the last ray at the end position (yes this could be optimized but i want to write out
// the logic fully so that it's clear what happens)
float spacing = distance / (totalRayCount - 1);
for (int i = 0; i < totalRayCount; i++)
{
// we can simply get the ray position by adding the direction multiplied by the spacing multiplied
// by the number of itterations we've gone through to the start position
// since the direction is normalized we can multiply it by the distance between rays to give it
// the length between two rays, if we then multiply it by the number of rays the current ray is,
// we get the distance as well as direction the current ray has to be placed away from the start
// position
Vector3 rayPosition = startRayPos + direction * spacing * i;
// i added some color so you can see start and end
if (i == 0)
Debug.DrawRay(startRayPos, Vector3.down, Color.green);
else if (i == totalRayCount - 1)
Debug.DrawRay(endRayPos, Vector3.down, Color.red);
else
Debug.DrawRay(rayPosition, Vector3.down, Color.white);
}
}
}
这是一个简单的逻辑错误。
您希望
sideRaysAmount
是光线的数量。但是您将其用作射线之间的步数。
=> 对于
step
的计算,您只想使用
float step = dist / (sideRaysAmount - 1) * i;