//一次环绕的射线次数
private int raycastCount = 1000;
//衰减百分比(比如打到一个物体,就衰减到0.9,二次就衰减到0.81)
private float attenuation = 0.9f;
//射线碰撞信息
private Dictionary<int, RaycastHit[]> raycastHitsMap;
/// <summary>
/// 绕一圈打出射线,记录每次打出射线的碰撞信息
/// </summary>
private void RaycastHitSphere(float maxRadius = 10000f)
{
raycastHitsMap = new Dictionary<int, RaycastHit[]>();
Vector3 raycastEndDir;
RaycastHit[] resultsTemp;
Quaternion quaternion = Quaternion.identity;
float pieceAngle = 360f / (raycastCount - 1);
for (int i = 0; i < raycastCount; i++)
{
quaternion = Quaternion.Euler(quaternion.eulerAngles.x, quaternion.eulerAngles.y + pieceAngle, quaternion.eulerAngles.z);
raycastEndDir = quaternion * transform.forward;
resultsTemp = Physics.RaycastAll(transform.position, raycastEndDir, maxRadius);
raycastHitsMap.Add(i, resultsTemp);
}
}
/// <summary>
/// 获取在参数半径下的所有位置被穿透次数和衰减比例
/// </summary>
/// <param name="radius"></param>
void SetAlpha(float radius)
{
for (int i = 0; i < raycastHitsMap.Count; i++)
{
RaycastHit[] hits = raycastHitsMap[i];
int raycastHitNum = 0;
for (int j = 0; j < hits.Length; j++)
{
if (Vector3.Distance(hits[j].transform.position, transform.position) < radius)
{
raycastHitNum++;
}
}
Debug.Log($"半径={radius},第{i}份,穿透{raycastHitNum}次,Alpha={Mathf.Pow(attenuation, raycastHitNum)}");
}
}
环绕打出射线,并根据碰撞次计算衰减比例
最新推荐文章于 2024-07-23 16:49:34 发布