0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Unity】敵の視野を疑似的に表示するギズモをつくった

Last updated at Posted at 2021-05-05

キャプチャ.PNG
よくある扇状の視野範囲をギズモで擬似的に表示できます。
検知の方法は偉大なるこちらの方の記事を参考にさせていただくと良いと思います。
: Unityで敵が主人公を検知する範囲を視覚内に限定する

Enemy_DetectionAI.cs
using UnityEngine;
public class Enemy_DetectionAI : MonoBehaviour
{   
    [SerializeField] private float searchRadius;
    [SerializeField] private float searchAngle = 130f;

   private void OnDrawGizmos()
    {
        Vector3 trans = transform.position;
        Gizmos.DrawWireSphere(trans, searchRadius);

        Gizmos.color = Color.red;
        float x, y, r, a;
        r = searchRadius;
        a = transform.eulerAngles.y + 90;
        x = r * Mathf.Cos(a * Mathf.PI / 180f);
        y = r * Mathf.Sin(a * Mathf.PI / 180f);
        Vector3 forwardPos = new Vector3(-x, 0, y);
        Gizmos.DrawSphere(trans + forwardPos, .1f);

        Gizmos.color = Color.green;
        float xr, yr;
        xr = r * Mathf.Cos((a + searchAngle/2) * Mathf.PI / 180f);
        yr = r * Mathf.Sin((a + searchAngle/2) * Mathf.PI / 180f);
        Vector3 rightPos = new Vector3(-xr, 0, yr);
        Gizmos.DrawSphere(trans + rightPos, .1f);

        Gizmos.color = Color.blue;
        float xl, yl;
        xl = r * Mathf.Cos((a - searchAngle/2) * Mathf.PI / 180f);
        yl = r * Mathf.Sin((a - searchAngle/2) * Mathf.PI / 180f);
        Vector3 leftPos = new Vector3(-xl, 0, yl);
        Gizmos.DrawSphere(trans + leftPos, .1f);

        Gizmos.color = Color.red;
        Gizmos.DrawLine(trans, trans + forwardPos);
        Gizmos.DrawLine(trans, trans + rightPos);
        Gizmos.DrawLine(trans, trans + leftPos);
    }
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?