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?

SAOのようなNPCだけの世界を作る - Part 2

Posted at

SAOのようなNPCだけの世界を作る - Part 2

はじめに

前回の記事では、NPCが自律的に行動し、環境に適応するシステムの基礎を紹介しました。
今回は、Ollama 3のEliza系統を活用した感情システムと会話システムの開発を行い、さらにNPCの行動をUnityに移行することを目指します。

また、NPCを自己進化型のAIとして構築し、学習と適応能力を持たせる方法についても解説します。

今回の目標

  • Unity上でNPCの行動を制御する
  • Ollama 3を活用した感情ベースの会話システムを実装する
  • NPCが環境や相手によって行動を変化させる
  • 自己進化型AIを導入し、NPCが成長するシステムを構築する

1. UnityでのNPC行動制御

NPCの基本的な移動と行動をC#で実装します。

1.1 Unityの環境セットアップ

  1. Unityをインストール(最新版推奨)
  2. 新規プロジェクトを作成(3D環境推奨)
  3. NPCController.cs を作成

1.2 NPCの移動スクリプト(C#)

using UnityEngine;
using System.Collections;

public class NPCController : MonoBehaviour
{
    public float speed = 2.0f;
    private Vector3 targetPosition;

    void Start()
    {
        SetNewTarget();
    }

    void Update()
    {
        MoveToTarget();
    }

    void SetNewTarget()
    {
        targetPosition = new Vector3(Random.Range(-10, 10), 0, Random.Range(-10, 10));
    }

    void MoveToTarget()
    {
        transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
        if (Vector3.Distance(transform.position, targetPosition) < 0.5f)
        {
            SetNewTarget();
        }
    }
}

2. Ollama 3を活用した会話システム

Ollama 3のEliza系統AIを利用し、NPCが感情を持った会話を行うようにします。

2.1 Ollama 3のセットアップ

  1. Ollama 3をインストール
  2. Elizaモデルをロード
  3. Pythonで会話システムを実装

2.2 NPCの感情システム(Python)

import ollama

class NPC:
    def __init__(self, name):
        self.name = name
        self.mood = "neutral"

    def chat(self, user_input):
        response = ollama.chat(model="eliza", messages=[
            {"role": "system", "content": f"NPCの感情: {self.mood}"},
            {"role": "user", "content": user_input}
        ])
        print(f"{self.name}: {response['message']}")
        self.adjust_mood(user_input)

    def adjust_mood(self, user_input):
        if "ありがとう" in user_input:
            self.mood = "happy"
        elif "嫌い" in user_input:
            self.mood = "angry"
        elif "さようなら" in user_input:
            self.mood = "sad"

npc = NPC("Alice")
npc.chat("こんにちは!")
npc.chat("ありがとう!")
npc.chat("嫌い!")

3. UnityとPython(Ollama 3)を連携

3.1 UnityでPythonスクリプトを実行

  1. PythonNet ライブラリを導入
  2. UnityからPythonの会話スクリプトを呼び出す

3.2 UnityからOllama 3を呼び出す(C#)

using UnityEngine;
using System.Diagnostics;

public class NPCChat : MonoBehaviour
{
    public void TalkToNPC(string input)
    {
        ProcessStartInfo startInfo = new ProcessStartInfo()
        {
            FileName = "python",
            Arguments = "npc_chat.py " + input,
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };

        Process process = new Process()
        {
            StartInfo = startInfo
        };

        process.Start();
        string response = process.StandardOutput.ReadToEnd();
        process.WaitForExit();

        UnityEngine.Debug.Log("NPC: " + response);
    }
}

4. 自己進化型AIの実装

NPCがプレイヤーとの会話や経験から学習し、成長する仕組みを構築します。

4.1 強化学習を活用したNPCの成長(Python)

import random

class LearningNPC:
    def __init__(self, name):
        self.name = name
        self.experience = {}

    def learn(self, situation, response):
        if situation not in self.experience:
            self.experience[situation] = []
        self.experience[situation].append(response)

    def respond(self, situation):
        if situation in self.experience:
            return random.choice(self.experience[situation])
        else:
            return "分からないな..."

npc = LearningNPC("Eve")
npc.learn("挨拶", "こんにちは!")
npc.learn("挨拶", "やあ!")
npc.learn("質問", "それについては考えたことがないな。")

print(npc.respond("挨拶"))
print(npc.respond("質問"))

今後の展望

  • NPCが会話の内容を記憶し、個別の性格を持つようにする
  • NPC同士が会話し、関係を築く機能を追加
  • より自然な表情アニメーションを導入し、リアルな感情表現を実現
  • NPCの行動が進化する自己学習型AIを強化

まとめ

今回は、

  • UnityでNPCの行動を制御する方法
  • Ollama 3のEliza系統を活用した会話システムの実装
  • UnityとPython(Ollama 3)の連携方法
  • 自己進化型AIの導入方法

を紹介しました。
次回は、NPCの記憶機能や学習機能をさらに強化し、より高度なAI世界を構築していきます!

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?