AIチュートリアル

# AIチュートリアル

## 参考URL
[ビヘイビアツリーのクイックスタート ガイド](https://docs.unrealengine.com/latest/JPN/Engine/AI/BehaviorTrees/QuickStart/index.html)

NavMesh コンテンツ サンプル
https://docs.unrealengine.com/latest/JPN/Resources/ContentExamples/NavMesh/index.html

## プロジェクトの作成
Blueprint TopDownを使用する

## Nav Mesh Bounds Volume
Model -> [Volumes] -> [Nav Mesh Bounds Volume]を画面にドラッグ

Pキーで表示/非表示切り替え

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/20767cee-bf52-67be-df2a-17649391a3af.png)

## 基本アセットの作成

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/ee874077-460b-3d4b-db97-65ad925bbd48.png)

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/b00b0ee2-a206-f904-3346-83dac4c45a86.png)


Blueprintsフォルダの作成

**BP_AIFollowerControllerを作成する**
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/d49780c7-8636-b8ad-5085-e10d7cf34366.png)

**BP_AICharacterを作成する**

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/8ba9ea20-9ec9-806f-1927-427e76f61ce2.png)

**BT_Followerを作成する**

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/ecd1b168-adf3-6797-ea67-fa2da468db6f.png)

**BB_Followerを作成する**
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/c9adc861-6036-5918-242b-f00356dbac27.png)


## BP_AICharacterのセットアップ
BP_AICharacterをダブルクリックで開く

Meshのスケルタルメッシュを[SK_Mannequin]に設定する
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/298bd0fb-a5f9-35d9-0d50-6b9ddb8d88a2.png)

**Transform**

|項目  |X  |Y  |Z  |
|---|---|---|---|
|Location  |0.0  |0.0  |-85.0  |
|Rotation  |0.0  |0.0 |-90.0 |
|Scale  |1.0  |1.0  |1.0  |

**Animation**

|項目  |値  |
|---|---|
|Animation Mode |Use Animation Blueprint  |
|Anim Class |ThirdPerson_AnimBP_C |

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/c3466fdc-7c4a-241b-5731-e1a34f8cb3c7.png)

**CapsuleComponentの大きさをキャラクターが入る大きさにする**

1. CapsuleComponentを選択する
2. Shapeの値を変更する

|項目  |値  |
|---|---|
|Capsule Half Height |96.0 |
|Capsule Radius |42.0 |

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/17d4f8c4-e3c8-68b0-c6c8-2c3668e10f77.png)

**CharacterMovementの設定を変更する**

|項目  |値  |
|---|---|
|Nav Agent Radius |42.0 |CapsuleComponent　Capsule Radius以上の値に設定する |
|Nav Agent Height |192.0 |CapsuleComponent　Capsule Half Heightの2倍以上の値に設定する|

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/fbbf6407-406f-d810-b462-93f7d5634f8f.png)


**Character Movement: Walking**

|項目  |値  |
|---|---|
|Max Walk Speed |400.0 |

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/beea969b-11d6-c8da-2b24-f64ebf7cad30.png)

**Class Defaultsをクリック**

[ブループリント エディタのデフォルト タブ](https://docs.unrealengine.com/latest/JPN/Engine/Blueprints/Editor/Modes/Defaults/DefaultsTab/index.html)

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/ced0f5c0-d384-7b1c-3260-57096367b6ce.png)

**AI Controller ClassにBP_AIFollowerControllerを設定する**
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/268f2ffd-d48b-b418-3b09-06c592c2a697.png)

**Compile -> Save**
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/9822e7f5-e12b-63d7-9cc6-1b8fb1b71fa6.png)

## BB_Followerの設定

**BB_Followerをダブルクリックして開く**
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/4ffbd5cb-ce9c-9142-d50d-99455cf85545.png)

**New KeyをクリックしてKey情報を追加する**

|Entry Name  |Key Type  |
|---|---|
|TargetToFollow |Object |
|HomeLocation |Vector |
|TargetLocation |Vector |

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/65a98e58-7d94-c9dd-7bf2-5ac6b10a61be.png)

**Save**
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/65678808-b811-f127-45fc-5ab8f50892d1.png)

## BP_AIFollowerControllerの設定

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/10e0efd8-7992-3850-86da-c297ee8e0c87.png)

**処理の実装**

**Use Blackboardを追加する**
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/8191d08c-888f-08b2-f0be-64db07a3445d.png)

**Blackboard AssetにBB_Followを設定する**
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/75363307-df47-9fe1-5516-546f647f60d4.png)

**ノードを追加する(Get Blackboard,Get Controlled Pawn)**
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/996395ae-521e-e594-676a-d25b983058c2.png)


![image](https://qiita-image-store.s3.amazonaws.com/0/111261/c861487b-8049-ae98-219a-92d8309ac727.png)

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/6558e417-a3ca-629f-b04c-bed1bba2adcc.png)

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/f6446032-621a-02fa-dcc1-ed55a2e5636c.png)

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/c0fa1c39-f7b2-782a-cbe3-ac7f16824182.png)

HomeLocationに値:HomeLocationを設定する
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/7bbc6f7b-84dc-c3f6-bd95-5948a3af79de.png)

VectorValueにGetLocalLocationのReturnをつなげる
Use Blackboardの実行とSet Value as Vectorの実行をつなげる
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/b3a53ca3-835e-7935-ce45-401d9234bc72.png)

Run Behavior Treeを追加する
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/c69189d3-d6ca-2869-d64d-f680274d89fb.png)

BTAssetにBT_Followerを選択する
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/7ec0a7b2-82d0-c5df-7c03-7bed0d43a35a.png)

Set value as VectorとRun Behavior Treeをつなげる
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/bddde58a-477b-771a-57e1-9f818143f2dd.png)

## BP_AICharacterを配置する
**BP_AICharacterを画面に配置する**
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/e4dc05a2-cc68-2ad0-8d91-a69b808c5bb0.png)

## BT_Followerを編集する
BT_Followerをダブルクリック
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/25781cca-c519-8efe-1933-795b15a3f5cb.png)

Composities -> Selectorを選択する
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/0b0f0f78-1b66-7d35-bf46-e336556daec4.png)

Composities -> Sequenceを選択する
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/f6cd1e27-6078-3946-1fec-f81481011d80.png)

Composities -> Selectorを選択する
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/ea132b16-182b-f716-8ec7-db1380f3b5c2.png)


![image](https://qiita-image-store.s3.amazonaws.com/0/111261/c6154ec2-be74-e685-da7b-fa4a13fb8343.png)

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/f6b04876-9cbb-0216-9d83-772547fbedb9.png)

## サービスの作成

Services は Composite ノードにアタッチされ、ブランチの実行中は定義された頻度で実行されるノードです。確認してブラックボードを更新するために頻繁に使用されます。他のビヘイビアツリー システムの通常の Parallel ノードの代わりになります。

BT_Followerを開いた状態で、[New Service]をクリックする
BTService_Bluprint Base Newというファイルが出来るので、名前をBTService_AgroCheckに設定する
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/7826e760-9d8c-7dc0-4259-9a267b56ddca.png)

### 変数を設定する

|変数名  |型  |
|---|---|
|DesiredObjectTypes |EObjectTypeQuery(配列) |
|MyLocation |Vector |
|AI_CON_Ref |BP_AIFollowerController(Reference) |
|TargetToFollow |BlackboardKeySelector |
|TargetLocation |BlackboardKeySelector |



![image](https://qiita-image-store.s3.amazonaws.com/0/111261/8a2839aa-728e-ea6f-800e-8ebfe948aafc.png)

DesiredObjectTypesの初期値を設定する
1. 配列を１つ追加する
2. Pawnを設定する
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/b693fc65-705e-3f3a-ff67-9f8de7ee3a4b.png)


![image](https://qiita-image-store.s3.amazonaws.com/0/111261/58910b82-485e-4fbe-2619-7a7ac428c344.png)


![image](https://qiita-image-store.s3.amazonaws.com/0/111261/09b34ea0-0706-6b41-1d44-c3492078e553.png)

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/c40f336a-76e8-5c60-e27f-ecf844728af8.png)

## タスクの作成

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/e487b6f1-8bec-250e-d2d4-df938990be9d.png)

### 変数の追加

|変数名  |型  |
|---|---|
|AI_CON_Ref |BP_AIFollowerController(Reference) |
|TargetToFollow |BlackboardKeySelector |

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/8a0ad12d-eb8f-f350-10b0-50a35c0ac200.png)

### 処理の実装
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/c35e4840-47e9-cf0e-0a37-0e6806ce7beb.png)


## デコレーターの作成
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/ffcd3c6a-4836-99f0-7b83-41558bd03ea7.png)

### 変数の追加

|変数名  |型  |
|---|---|
|AI_CON_Ref |BP_AIFollowerController(Reference) |
|AcceptableDistance |Float |
|TargetToFollow |BlackboardKeySelector |

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/a1f90ee2-66ba-623b-4757-3920995f31a3.png)

### 関数の追加と実装

Function [Override] -> [Perform Condition Check]を選択する
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/51e7e0f9-5bb4-0dbb-b4f9-e43499e25426.png)

**処理の実装**
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/a052341d-70c2-ed8c-619b-4e81a3798b8d.png)

## ビヘイビアツリーのビルド
### サービスを追加

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/dff3c380-214a-8201-e9ef-4fbca2d3d366.png)

**Default**

|項目  |値  |
|---|---|
|Target to Follow |TargetToFollow |
|Target Location |TargetLocation |

**Service**

|項目  |値  |
|---|---|
|Interval |0.5 |
|Random Devlation |0.0 |


![image](https://qiita-image-store.s3.amazonaws.com/0/111261/55a86a1a-8346-1616-7814-19ab892106fd.png)

### タスクを追加する
**BTTask Rapid Move Toを追加**
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/0b6731c1-0f1b-2058-79de-116680f33f97.png)

**Move Toを追加**
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/029a1b3c-4b5b-e526-35f3-2e668d66a587.png)

**Waitを追加**
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/a06a3ef9-3d26-fced-b30e-909a219f9fca.png)

**Move Toを追加**
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/f30c2d6f-e608-54fd-98bb-5601fb5d6a65.png)

**追加したタスクを接続する**

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/0373c3e3-58e6-5200-cdf3-bb9033a8a7f2.png)

**Move ToのBlackboardを選択する**
左側: Target Location
右側: Home Location
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/23fa7028-9c95-9717-513e-fe5968ac8387.png)

**Waitの詳細を設定する**

**Wait**

|項目  |値  |
|---|---|
|Wait Time |0.25 |

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/2408410f-84c5-321a-2bfc-c3b75c608c58.png)

**BTTask_RapidMoveToの詳細を設定する**

**Default**

|項目  |値  |
|---|---|
|Target to Follow |TargetToFollow |

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/9002d735-5461-b3bf-6745-af20cd2e69e1.png)


### デコレーターを追加する

#### Selectorのデコレーターを追加する
**Blackboardを追加する**
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/fcc2659a-d48c-f639-e216-777d09c3b2ef.png)

**BTDecorator Close Enoughを追加する**
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/562de958-7bc8-9621-3a5c-1113462b689b.png)

**Selector：Blackboardの詳細を設定する**

**Default**

|項目  |値  |
|---|---|
|Observer Aborts |Both |

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/6d67cef3-84d4-aa6b-fc70-625b8289f3c7.png)

**Selector：BTDecorator Close Enoughの詳細を設定する**

|項目  |値  |
|---|---|
|Acceptable Distance  |100 |
|Target to Follow |TargetToFollow |

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/437c92a5-746c-e472-9e5b-a5bf6185aaf1.png)

#### Sequenceのデコレーターを追加する

**Blackboardを追加する**

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/5194a1d8-9ca0-eb19-8b7b-7313a5ce79f7.png)

**Is at Locationを追加する**
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/2687f700-a292-5444-fd4c-88a717ac0ae4.png)

**Blackboardを追加する**
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/c8f3b749-97e2-7c72-55db-ea5cbbc8a373.png)

**Blackboard（上）の詳細を設定する**

**Brackboard**

|項目  |値  |
|---|---|
|Key Query |Is Not Set |

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/cfcd6158-85bf-84cc-28ad-09767a61382f.png)

**Blackboard（下）の詳細を設定する**
**Brackboard**

|項目  |値  |
|---|---|
|Blackboard Key |HomeLocation |

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/5e90af9f-f8d7-b3a3-549a-bef3c695f604.png)

**Is At Locationの詳細を設定する**

**Condition**

|項目  |値  |
|---|---|
|Inverse Condition |チェック |

**Blackboard**

|項目  |値  |
|---|---|
|Blackboard Key |HomeLocation |

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/fd9645ff-2a74-7cbf-a5ec-66631d35bba4.png)