我们可以与脚本统一创建像Gizmos一样的圆锥体吗

时间:2018-09-01 18:20:26

标签: unity3d

我想使用脚本创建一个圆锥形的Gizmo,如下图所示。

这是示例代码:

void OnDrawGizmosSelected()
{
    Gizmos.color = Color.red;
    Gizmos.DrawWireSphere(transform.position, range);
}

红线是我想要的Gizmo。 gizmos

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

下面的代码是Tetrad对similar question的回答的修改版本。

void OnDrawGizmosSelected()
{
    float angle = 30.0f;
    float rayRange = 10.0f;
    float halfFOV = angle / 2.0f;
    float coneDirection = 180;

    Quaternion upRayRotation = Quaternion.AngleAxis(-halfFOV + coneDirection, Vector3.forward);
    Quaternion downRayRotation = Quaternion.AngleAxis(halfFOV + coneDirection, Vector3.forward);

    Vector3 upRayDirection = upRayRotation * transform.right * rayRange;
    Vector3 downRayDirection = downRayRotation * transform.right * rayRange;

    Gizmos.DrawRay(transform.position, upRayDirection);
    Gizmos.DrawRay(transform.position, downRayDirection);
    Gizmos.DrawLine(transform.position + downRayDirection, transform.position + upRayDirection);
}

希望有帮助!