はじめに
Unity 6.2 betaで、AIとチャットしながら開発が出来る Unity AI の Assistant が使用できるようになりました。
このAssistantの学習するため、Unityのチュートリアルでお馴染みのRoll-a-ballの作成を試しましたので、その備忘録となります。
Unity AI Assistant について
Assistantは以下の3つの機能があります。今回は、run、codeを駆使して作成します。
- /askモード: プロジェクト ファイルを変更せずに、回答を提供したり、ドキュメントを参照したり、プロジェクト固有の詳細を取得したりします。
- /runモード: Unity エディターで直接変更を実行できる機能スクリプトを生成することで、反復タスクを自動化します。
- /codeモード: Unity API と対話するカスタマイズ可能な C# コード スニペットを生成し、レビューします。
引用 : https://docs.unity3d.com/Packages/com.unity.ai.assistant@1.0/manual/index.html
環境
- Unity : 6000.2.0b7
- Package
- Assistant : 1.0.0-pre.10
- AI Toolkit : 1.0.0-pre.16
Assistant インストール
- Unity のエディターの左上に"AI"アイコンを選択
- "Agree and install Unity AI"を選択するとパッケージのインストールが開始される
インストールが完了すると、再度"AI"を選択して、"Open Assistant"を選択してAssistantのウインドウが表示される。
作業内容
※以下に記述する内容は、いろいろ試行錯誤を経た結果となってます。
まずはじめに、必要な設定と素材の作成をします。これを事前に行うのは、この後のAIへのリクエストの効率化と一度にリクエストしないとうまくいかない個所があったからです。
1.タグ"PickUp"の追加
/run
- タグに 'PickUp' を追加してください
2.Playerのスクリプトを作成
/code
# 作成するゲーム
Roll-a-ballを作成する
# 作業内容
以下の内容の"PlayerController"スクリプトを作成してください
- InputSystemの"Send Message"に対応するOnMoveを追加する
- OnTriggerEnterを追加する
- OnTriggerEnterでタグ"PickUp"にhitしたらhitしたオブジェクトを非表示にする
- 獲得数をカウントするカウンターを作る
- PickUpにカウンターをTextMeshProTextに表示を設定するようにする
- speed の値は 10
生成コード
// 2025/07/26 AI-Tag
// This was created with the help of Assistant, a Unity Artificial Intelligence product.
using UnityEngine;
using UnityEngine.InputSystem;
using TMPro;
public class PlayerController : MonoBehaviour
{
public float speed = 10f; // Speed of the player
public TextMeshProUGUI countText; // UI element to display the count
private Rigidbody rb; // Rigidbody component of the player
private int count; // Counter to keep track of collected items
private Vector2 moveInput; // Input vector for movement
void Start()
{
rb = GetComponent<Rigidbody>(); // Get the Rigidbody component
count = 0; // Initialize the counter to 0
UpdateCountText(); // Update the UI text
}
void FixedUpdate()
{
// Apply movement based on input
Vector3 movement = new Vector3(moveInput.x, 0.0f, moveInput.y);
rb.AddForce(movement * speed);
}
void OnMove(InputValue movementValue)
{
// Get the input vector from the Input System
Vector2 inputVector = movementValue.Get<Vector2>();
moveInput = inputVector;
}
void OnTriggerEnter(Collider other)
{
// Check if the collided object has the "PickUp" tag
if (other.gameObject.CompareTag("PickUp"))
{
// Deactivate the object
other.gameObject.SetActive(false);
// Increment the count and update the UI
count++;
UpdateCountText();
}
}
void UpdateCountText()
{
// Update the TextMeshPro text with the new count
countText.text = "Count: " + count.ToString();
}
}
3.Cameraのスクリプトを作成
/code
# 作成するゲーム
Roll-a-ballを作成する
# 作業内容
"CameraController"スクリプトを作成してください
生成コード
// 2025/07/26 AI-Tag
// This was created with the help of Assistant, a Unity Artificial Intelligence product.
using UnityEngine;
public class CameraController : MonoBehaviour
{
public GameObject player; // Reference to the player GameObject
private Vector3 offset; // Offset distance between the player and camera
void Start()
{
// Calculate and store the offset value by getting the distance between the player's position and camera's position
offset = transform.position - player.transform.position;
}
void LateUpdate()
{
// Set the position of the camera to the player's position plus the offset
transform.position = player.transform.position + offset;
}
}
4.PickUpのスクリプトを作成
/code
# 作成するゲーム
Roll-a-ballを作成する
# 作業内容
以下の内容の"PickUp"スクリプトを作成してください
- 常に回転するようにする
生成コード
// 2025/07/26 AI-Tag
// This was created with the help of Assistant, a Unity Artificial Intelligence product.
using UnityEngine;
public class PickUp : MonoBehaviour
{
public float rotationSpeed = 100f; // Rotation speed in degrees per second
void Update()
{
// Rotate the object around its Y-axis
transform.Rotate(Vector3.up, rotationSpeed * Time.deltaTime);
}
}
5.マテリアルの作成
/run
# 作成するゲーム
Roll-a-ballを作成する
# 作業内容
Assets/MaterialsにURP対応の以下のMaterialを作成してください
- Name:Player Color:水色
- Name:Walls Color:灰色
- Name:PickUp Color:黄色
ここから、シーンにオブジェクトの作成を行っていきます。
6.ステージオブジェクトの作成
/run
# 作成するゲーム
Roll-a-ballを作成する
# 作業内容
- 床、四方を囲った4つの壁のあるステージを作成し、作成したオブジェクトにWallsマテリアルをアタッチする
7.PickUpオブジェクトの作成
/run
# 作成するゲーム
Roll-a-ballを作成する
# 作業内容
- Cubeオブジェクトで"PickUp"を作成する
- シーン内に複数のPickUpをランダムな位置に配置してください
- PickUpオブジェクトを斜めに回転させる
- PickUpオブジェクトにPickUpスクリプトをアタッチする
- PickUpオブジェクトにタグを"PickUp"に変更する
- PickUpオブジェクトのBoxColliderのIsTriggerをOnにする
- PickUpマテリアルをつける
8.PickUpの獲得数の表示UIの作成
/run
# 作成するゲーム
Roll-a-ballを作成する
# 作業内容
- GUIを表示するCanvasを追加する
- PickUpを獲得したカウンターを表示するGUIの"TextMeshPro Text"を追加する
- Textの色を白にする
- Textは左上に配置する
9.Playerオブジェクトの作成
/run
# 作成するゲーム
Roll-a-ballを作成する
# 作業内容
- Sphereオブジェクトで"Player"を作成する
- "Rigidbody"と"PlayerInput"をつける
- Playerマテリアルをつける
- PlayerControllerスクリプトをつける
10.PlayerControllerにGUIのTextを割り当てる
/run
# 作成するゲーム
Roll-a-ballを作成する
# 作業内容
- シーン上のPlayerオブジェクトのPlayerControllerのインスペクター上のTextMeshProUGUI型の"countText"に、シーン上の"CountText"オブジェクトをアタッチして
9のPlayer作成と分けていたのは、うまくいかなかったので分けて作成しました。
11.Cameraを設定
/run
# 作成するゲーム
Roll-a-ballを作成する
# 作業内容
- Main Cameraをプレイヤーを追従する位置にする
- Main Cameraオブジェクトに"CameraController"スクリプトをアタッチする
- Main CameraオブジェクトのCameraControllerのインスペクター上の"player"に、Playerオブジェクトをアタッチする
完成品
よくある/runのCommand logicエラー対応
MaterialのエラーCS0118
Material
=>UnityEngine.Material
に修正する。
MonoScriptのエラーCS0246
using UnityEditor;
を追加する。
うまくいかなかった箇所
/runでPicUpなどの複数のオブジェクトを一括で、変更するのがうまくいかず、PickUp作成時に設定するようにしました。