视频效果:
代码:
cs
/// <summary>
/// 投影在扇形区域内的点
/// </summary>
/// <param name="targetPos">目标点</param>
/// <param name="fanRadius">扇形半径</param>
/// <param name="fanAngle">扇形圆心角</param>
/// <param name="fanShape">扇形(扇形圆心)</param>
/// <returns></returns>
private Vector3 GetPosToFanProjection(Vector3 targetPos,float fanRadius,float fanAngle,Transform fanShape)
{
Vector3 leftdir = Quaternion.AngleAxis(-fanAngle/2, fanShape.transform.up)*fanShape.transform.forward;
Vector3 rightdir = Quaternion.AngleAxis(fanAngle/2, fanShape.transform.up)*fanShape.transform.forward;
Vector3 leftP = fanShape.transform.position + leftdir * fanRadius;
Vector3 rightP = fanShape.transform.position + rightdir * fanRadius;
float dis = (targetPos - fanShape.transform.position).magnitude;
float angle = Vector3.SignedAngle(fanShape.transform.forward, targetPos - fanShape.transform.position,fanShape.transform.up);
if (dis > fanRadius)
{
if (Mathf.Abs(angle) < 0.5f * fanAngle)
{
return fanShape.transform.position + (targetPos - fanShape.transform.position).normalized * this.fanRadius;
}
else
{
//右顶点
if (angle > 0)
{
return rightP;
}
//左顶点
else
{
return leftP;
}
}
}
else
{
if (Mathf.Abs(angle) < 0.5f * fanAngle)
{
return targetPos;
}
else
{
//右边界
if (angle > 0)
{
return fanShape.transform.position + rightdir * dis;
}
//左边界
else
{
return fanShape.transform.position + leftdir * dis;
}
}
}
}