---
title: "Unity のナビメッシュ"
content_type: tutorial
url: "https://learn.unity.com/tutorial/unity-navmesh-jp?language=ja"
language: ja
difficulty: beginner
duration_minutes: 35
unity_version:
  - "2017.3"
supported_unity_versions:
  - name: "2017.3"
    url: "https://learn.unity.com/tutorial/unity-navmesh-jp.md?language=ja&version=2017.3"
last_updated: "2026-08-04T14:23:02.000Z"
---

# Unity のナビメッシュ

Brackeys 制作の動画で、Unity の NavMesh コンポーネントを使った AI 経路探索の作成方法を学びます。

Unity の NavMesh コンポーネントを使用して AI による経路探索を作成する方法を学びましょう！

この動画は Brackeys によって制作されました。

## ナビメッシュの基礎

Unity の NavMesh コンポーネントを使った AI による経路探索の作成方法を学びます。

この動画は Brackeys によって制作されました。

[Video](http://www.youtube.com/watch?v=CHV1ymlw-P8)

サンプルプロジェクトのダウンロードは[こちら](https://github.com/Brackeys/NavMesh-Tutorial)から！

## PlayerController

```csharp
using UnityEngine;
using UnityEngine.AI;

public class PlayerController : MonoBehaviour {

    public Camera cam;

    public NavMeshAgent agent;

    // Update is called once per frame
    void Update () 
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = cam.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                agent.SetDestination(hit.point);
            }
        }
    }
}
```

## 動的に生成する

ランタイムでナビメッシュを更新する方法を学びます。

 この動画は Brackeys によって制作されました。

[Video](http://www.youtube.com/watch?v=FkLJ45Pt-mY)

## ObstacleAnimation

```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObstacleAnimation : MonoBehaviour {

    public float speed = .2f;
    public float strength = 9f;

    private float randomOffset;

    // Use this for initialization
    void Start () {
        randomOffset = Random.Range(0f, 2f);
    }

    // Update is called once per frame
    void Update () {
        Vector3 pos = transform.position;
        pos.x = Mathf.Sin(Time.time * speed + randomOffset) * strength;
        transform.position = pos;
    }
}
```

## LevelGenerator

```csharp
using UnityEngine;

public class LevelGenerator : MonoBehaviour {

        public NavMeshSurface surface;
    public int width = 10;
    public int height = 10;

    public GameObject wall;
    public GameObject player;

    private bool playerSpawned = false;

    // Use this for initialization
    void Start () {
        GenerateLevel();
                surface.BuildNavMesh();
    }

    // Create a grid based level
    void GenerateLevel()
    {
        // Loop over the grid
        for (int x = 0; x <= width; x+=2)
        {
            for (int y = 0; y <= height; y+=2)
            {
                // Should we place a wall?
                if (Random.value > .7f)
                {
                    // Spawn a wall
                    Vector3 pos = new Vector3(x - width / 2f, 1f, y - height / 2f);
                    Instantiate(wall, pos, Quaternion.identity, transform);
                } else if (!playerSpawned) // Should we spawn a player?
                {
                    // Spawn the player
                    Vector3 pos = new Vector3(x - width / 2f, 1.25f, y - height / 2f);
                    Instantiate(player, pos, Quaternion.identity);
                    playerSpawned = true;
                }
            }
        }
    }
}
```

## アニメーションキャラクター

サーフェスをリンクする方法や、エージェントにアニメーションキャラクターを追加する方法を学びます。

この動画は Brackeys によって制作されました。

[Video](http://www.youtube.com/watch?v=blPglabGueM)

## PlayerController

```csharp
using UnityEngine;
using UnityEngine.AI;
using UnityStandardAssets.Characters.ThirdPerson;

public class PlayerController : MonoBehaviour {

    public Camera cam;

    public NavMeshAgent agent;

    public ThirdPersonCharacter character;

    void Start()
    {
        agent.updateRotation = false;
    }

    // Update is called once per frame
    void Update () 
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = cam.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                agent.SetDesitnation(hit.point);
            }
        }

        if (agent.remainingDistance > agent.stoppingDistance)
        {
            character.Move(agent.desiredVelocity, false, false);
        }
        else
        {
            character.Move(Vector3.zero, false, false);
        }

    }
}
```
