UnityでDrawLineを使って擬似的にスフィアを描画する

初めに

Unityでrayの当たり判定でデバッグをする際に Gizmoや Drawlineを使って行うことが多々あります。今回は Gizmoが使えない(MonoBehaviourを継承していない or 更新関数を OnDrawGizmosまで伝播できない)場合の方法として Drawlineを使ったスフィア実装を行なってみます

Demo

以下でリポジトリを公開しています

開発環境

  • Unity 2022.3.x

実装

Cos,Sinを使って円形を擬似的に作成して描画をしています

    private void DrawDebugSphere(Vector3 position, float radius, Color color, int segments = 12)
    {
        // 経度方向の円(水平)
        for(int i = 0; i < segments; i++)
        {
            float theta1 = (i * Mathf.PI * 2) / segments;
            float theta2 = ((i + 1) * Mathf.PI * 2) / segments;

            Vector3 pos1 = new Vector3(radius * Mathf.Cos(theta1), 0, radius * Mathf.Sin(theta1)) + position;
            Vector3 pos2 = new Vector3(radius * Mathf.Cos(theta2), 0, radius * Mathf.Sin(theta2)) + position;

            Debug.DrawLine(pos1, pos2, color);
        }

        // 緯度方向の円(垂直)
        for(int i = 0; i < segments; i++)
        {
            float theta1 = (i * Mathf.PI * 2) / segments;
            float theta2 = ((i + 1) * Mathf.PI * 2) / segments;

            Vector3 pos1 = new Vector3(0, radius * Mathf.Cos(theta1), radius * Mathf.Sin(theta1)) + position;
            Vector3 pos2 = new Vector3(0, radius * Mathf.Cos(theta2), radius * Mathf.Sin(theta2)) + position;

            Debug.DrawLine(pos1, pos2, color);
        }

        // 斜め方向の円(経線)
        for(int i = 0; i < segments; i++)
        {
            float theta1 = (i * Mathf.PI * 2) / segments;
            float theta2 = ((i + 1) * Mathf.PI * 2) / segments;

            Vector3 pos1 = new Vector3(radius * Mathf.Cos(theta1) * Mathf.Sin(Mathf.PI / 4), radius * Mathf.Cos(theta1) * Mathf.Cos(Mathf.PI / 4), radius * Mathf.Sin(theta1)) + position;
            Vector3 pos2 = new Vector3(radius * Mathf.Cos(theta2) * Mathf.Sin(Mathf.PI / 4), radius * Mathf.Cos(theta2) * Mathf.Cos(Mathf.PI / 4), radius * Mathf.Sin(theta2)) + position;

            Debug.DrawLine(pos1, pos2, color);
        }
    }