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のAI AssistantでRoll-a-ballの作成を試す

Posted at

はじめに

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 インストール

  1. Unity のエディターの左上に"AI"アイコンを選択
  2. "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
生成コード
PlayerController.cs
// 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"スクリプトを作成してください
生成コード
CameraController.cs
// 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"スクリプトを作成してください
- 常に回転するようにする
生成コード
PickUp.cs
// 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オブジェクトをアタッチする

スクリーンショット

完成品

Movie

よくある/runのCommand logicエラー対応

MaterialのエラーCS0118

Material=>UnityEngine.Material に修正する。

MonoScriptのエラーCS0246

using UnityEditor; を追加する。

うまくいかなかった箇所

/runでPicUpなどの複数のオブジェクトを一括で、変更するのがうまくいかず、PickUp作成時に設定するようにしました。

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?