0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Unity 2D] NavMeshPlusでターゲットを追跡する敵を作る

Posted at

1. NavMeshPlusをインポート

まずは以下のリポジトリからzipをダウンロードしてください。

  • 「Code」を選択し、「Download ZIP」を選択するとダウンロードできます


ダウンロード完了後、各フォルダ「Gizmos」「NavMeshComponents」をプロジェクトのAssets配下にインポートしてください。
インポート後、Unityのパッケージマネージャーから「AI Navigation」をインストールしてください。

00AI Navi.png

2. NavMeshを生成

簡単にステージを作成します。

  • 四角形の2Dオブジェクトで地面(歩行可能なスペース)とブロック(歩行不可なスペース)を作成
  • ターゲット(赤)と敵(青)のオブジェクトを作成

01stage.png


地面とブロックにコンポーネント「Navigation Modifier」をアタッチし、override areaにチェックを入れます。 地面はWalkable、ブロックはNot Walkableを選択です。

02ground.png

03block.png

ブロックのコンポーネントは「Nav Mesh Obstacle」でもいいですが、ベイクでNavMeshを生成したときに、角が角ばってしまったり、動的に移動させたいわけではないので、「Navigation Modifier」にしてあります。


[任意] window -> AI -> Navigationを選択し、新しくAgent typeを作成。

04agent.png


空のオブジェクトを作成し、各コンポーネント「Navigation Surface」「Navigation CollectSources2d」をアタッチ。
Agent typeIDに先ほど作成したAgent typeを設定。

05navmesh.png


「Navigation CollectSources2d」のRotate Surface to XYを1回だけ実行します。 実行すると、オブジェクトのRotationXが-90になり、2Dでベイクができるようになります。
後は「Navigation Surface」のベイクを実行でNavMeshが生成され、青色の道が表示されるはずです。

beak.png

追跡をさせてみる

先ほど作成したターゲットのオブジェクトに以下のスクリプトを作成してアタッチします。

qiita.rb
using UnityEngine;

public class Target : MonoBehaviour
{
    void Update()
    {
        var mousePos = Input.mousePosition;
        var pos = Camera.main.ScreenToWorldPoint(mousePos);
        transform.position = new Vector2(pos.x, pos.y);
    }
}


次に敵のオブジェクトに各コンポーネント「Nav Mesh Agent」「Circle Collider 2D」をアタッチ 「Nav Mesh Agent」の半径を「Circle Collider 2D」の半径に揃えてください。

06enemy.png


続いてターゲットを追跡するスクリプトを作成し、敵のオブジェクトにアタッチします。
qiita.rb
using UnityEngine;
using UnityEngine.AI;

public class Enemy : MonoBehaviour
{
    [SerializeField] Transform target;
    NavMeshAgent agent;

    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        agent.updateRotation = false;
        agent.updateUpAxis = false;
    }

    void Update()
    {
        agent.destination = target.position;
    }

    void OnDrawGizmos()
    {
        if(!agent || !agent.enabled) return;
        Gizmos.color = Color.red;
        Vector2 startPos = transform.position;
        foreach (var endPos in agent.path.corners)
        {
            Gizmos.DrawLine(startPos, endPos);
            startPos = endPos;
        }
    }
}

OnDrawGizmosは省略可能

作成が完了したら、targetの変数にターゲットのオブジェクトを設定して完了です。
実行してみるとマウスカーソルに向かって敵が追跡してくるようになります。

hoge.gif

3. 他にサイズが異なる敵を作成したい場合

  • 新しくAgent typeを作成し、半径を調整します
  • NavMeshを作成したオブジェクトを複製し、対象のAgent typeIDを変更後、ベイクを実行します
  • あとはNavMeshAgentに作成したAgent typeを設定するのを忘れないで下さい
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?