iT邦幫忙

2025 iThome 鐵人賽

DAY 23
0
Software Development

Vibe Unity - AI時代的遊戲開發工作流系列 第 23

Day 23 - 如何讓 AI 控制遊戲裡的天氣 ?

  • 分享至 

  • xImage
  •  

image_1.jpg

如何讓 AI 可以根據情況來控制遊戲的場景
更改天氣, 下雪, 下雨, 或是打雷 ⚡️

以我正在開發的阿卡西AI遊戲為例
要實現AI自動化控制遊戲的場景
需要先跟 AI 定義好來回溝通的 Json 格式:

AI 回傳這個指令:

{
  "intent": "ChangeWeather",
  "weather": "Rain",        // 可用:Clear, Rain, Snow, Thunder
  "intensity": 0.8          // 0~1, 可選
}

執行完之後回傳這個給 AI :

{
  "status": "Success",
  "currentWeather": "Rain"
}

在 Unity 中簡易的測試程式碼:

using UnityEngine;

[System.Serializable]
public class WeatherCommand
{
    public string intent;
    public string weather;
    public float intensity;
}

public class SimpleWeatherController : MonoBehaviour
{
    public ParticleSystem rainFX;
    public ParticleSystem snowFX;
    public Light sunLight;
    public AudioSource thunderSFX;

    string currentWeather = "Clear";

    public string ApplyAICommand(string json)
    {
        WeatherCommand cmd = JsonUtility.FromJson<WeatherCommand>(json);

        if (cmd.intent == "ChangeWeather")
        {
            ChangeWeather(cmd.weather, cmd.intensity);
            return JsonUtility.ToJson(new { status = "Success", currentWeather = currentWeather });
        }

        return JsonUtility.ToJson(new { status = "UnknownCommand" });
    }

    void ChangeWeather(string type, float intensity)
    {
        // 關閉所有效果
        if (rainFX) rainFX.Stop();
        if (snowFX) snowFX.Stop();
        if (thunderSFX) thunderSFX.Stop();

        switch (type)
        {
            case "Clear":
                RenderSettings.skybox.color = Color.cyan;
                sunLight.intensity = 1.0f;
                break;

            case "Rain":
                if (rainFX) rainFX.Play();
                sunLight.intensity = 0.6f;
                break;

            case "Snow":
                if (snowFX) snowFX.Play();
                sunLight.intensity = 0.7f;
                break;

            case "Thunder":
                if (rainFX) rainFX.Play();
                sunLight.intensity = 0.5f;
                if (thunderSFX) thunderSFX.Play();
                break;
        }

        currentWeather = type;
        Debug.Log($"🌤 Weather changed to {type}");
    }
}

當 AI 回傳的 Json 經過解析之後
我們就可以執行 ChangeWeather 的功能來進行改編遊戲天氣的效果啦


上一篇
Day 22 - 讓 AI 控制你的遊戲角色 - In Game MCP
下一篇
Day 24 - AI 記憶係統的開發原理
系列文
Vibe Unity - AI時代的遊戲開發工作流25
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言