炸弹投掷路线原理
作者:互联网
炸弹投掷路线主要还是使用射线来进行生成,原理是计算当前的投掷方向,往前发射一段距离,判断是否有触碰到物体,如果这个时候没有碰到物体则重新计算方向,当前地点,继续向计算好的方向发射射线,直到射触碰到到物体而已,当然也不能无止境去计算去改变方向,必须设置一个长度,某则在某些情况会导致卡顿设置内存溢出。把其中的每次改变方向的坐标的存储起来,用LineRenderer来进行画出轨迹,最后当要投掷出炸弹时,让炸弹根据得到的List进行移动就行了。下面放核心代码。
bool targetAcquired;
public LineRenderer laser;
public int laserSteps;//射线拐点总数
public float dropPerSegment;//每段下降距离
public float laserSegmentDistance;//每段射线发射距离
public int collisionLayer;
public Vector3 targetPos;
public void TrajectoryCalculation()
{
targetAcquired = false;
RaycastHit hit;
Vector3 origin = transform.position;
laser.SetPosition(0, origin);
for (int i = 0; i < laserSteps - 1; i++)
{
Vector3 offset = (transform.forward + (Vector3.down * dropPerSegment * i)).normalized * laserSegmentDistance;
if (Physics.Raycast(origin, offset, out hit, laserSegmentDistance))
{
for (int j = i + 1; j < laser.positionCount; j++)
{
laser.SetPosition(j, hit.point);
}
if (hit.transform.gameObject.layer == collisionLayer)
{
laser.startColor = laser.endColor = Color.green;
targetPos = hit.point;
targetAcquired = true;
return;
}
else
{
laser.startColor = laser.endColor = Color.red;
return;
}
}
else
{
laser.SetPosition(i + 1, origin + offset);
origin += offset;
}
}
laser.startColor = laser.endColor = Color.red;
}
标签:origin,hit,int,投掷,路线,炸弹,offset,laser,public 来源: https://blog.csdn.net/qq1342753906/article/details/115379918