家事ストレスをゼロへ!タスク管理のプロが推す自動化家電TOP7
きたたく
きたたくブログ
あなたはUnityのプロフェッショナルなゲーム開発者です。C#を使用して、シンプルな生態系シミュレーションを作成してください。ゲームは以下の要件を満たす必要があります。
このプロジェクトは異なるAIモデルで比較するため、以下の点に注意すること:
完全なUnity C#コードを出力してください。
このコードを基に、開発者が簡単にシミュレーションを実行できるようにしてください。
ここに、Unityで生態系シミュレーションを作成するための完全なC#スクリプトを用意しました。
このシミュレーションでは、エージェント(動物)が食料を探し、エネルギー管理を行いながら生存・繁殖・死亡を繰り返します。
GameManager(空のGameObject)AnimalPrefab(エージェント用のプレハブ)FoodPrefab(食料用のプレハブ)Planeを選択し、NavigationタブでStaticにチェックを入れ、BakeをクリックGameManager.cs → GameManagerオブジェクトにアタッチAnimal.cs → AnimalPrefabにアタッチFood.cs → FoodPrefabにアタッチGameManager.cs(シミュレーションの管理)using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
public GameObject animalPrefab;
public GameObject foodPrefab;
public int initialAnimalCount = 10;
public int foodSpawnRate = 5; // 何秒ごとに食料を生成するか
public int maxFood = 20;
public Vector2 fieldSize = new Vector2(20, 20);
private List<GameObject> animals = new List<GameObject>();
private List<GameObject> foods = new List<GameObject>();
void Start()
{
// 初期エージェントを生成
for (int i = 0; i < initialAnimalCount; i++)
{
SpawnAnimal();
}
// 食料を定期的に生成
StartCoroutine(SpawnFoodRoutine());
}
void Update()
{
// エージェントが全滅したらシミュレーション終了
if (animals.Count == 0)
{
Debug.Log("Simulation Ended: All animals have died.");
StopAllCoroutines();
}
}
void SpawnAnimal()
{
Vector3 position = new Vector3(Random.Range(-fieldSize.x, fieldSize.x), 1, Random.Range(-fieldSize.y, fieldSize.y));
GameObject animal = Instantiate(animalPrefab, position, Quaternion.identity);
animals.Add(animal);
}
IEnumerator SpawnFoodRoutine()
{
while (true)
{
yield return new WaitForSeconds(foodSpawnRate);
if (foods.Count < maxFood)
{
SpawnFood();
}
}
}
void SpawnFood()
{
Vector3 position = new Vector3(Random.Range(-fieldSize.x, fieldSize.x), 0.5f, Random.Range(-fieldSize.y, fieldSize.y));
GameObject food = Instantiate(foodPrefab, position, Quaternion.identity);
foods.Add(food);
}
public void RemoveAnimal(GameObject animal)
{
animals.Remove(animal);
}
public void RemoveFood(GameObject food)
{
foods.Remove(food);
}
}
Animal.cs(エージェントの行動AI)using System.Collections;
using UnityEngine;
using UnityEngine.AI;
public class Animal : MonoBehaviour
{
public float energy = 100f;
public float energyLossRate = 1f;
public float energyGain = 30f;
public float reproductionThreshold = 120f;
public float detectionRange = 10f;
private NavMeshAgent agent;
private GameManager gameManager;
void Start()
{
agent = GetComponent<NavMeshAgent>();
gameManager = FindObjectOfType<GameManager>();
StartCoroutine(BehaviorLoop());
}
IEnumerator BehaviorLoop()
{
while (energy > 0)
{
// 食料を探す
GameObject food = FindNearestFood();
if (food != null)
{
agent.SetDestination(food.transform.position);
while (Vector3.Distance(transform.position, food.transform.position) > 1f)
{
energy -= energyLossRate * Time.deltaTime;
yield return null;
}
EatFood(food);
}
else
{
// ランダム移動
Vector3 randomPosition = new Vector3(Random.Range(-10, 10), 1, Random.Range(-10, 10));
agent.SetDestination(randomPosition);
yield return new WaitForSeconds(3);
}
// 繁殖チェック
if (energy >= reproductionThreshold)
{
Reproduce();
}
// 死亡チェック
if (energy <= 0)
{
Die();
}
}
}
GameObject FindNearestFood()
{
GameObject[] foods = GameObject.FindGameObjectsWithTag("Food");
GameObject nearestFood = null;
float minDistance = detectionRange;
foreach (GameObject food in foods)
{
float distance = Vector3.Distance(transform.position, food.transform.position);
if (distance < minDistance)
{
minDistance = distance;
nearestFood = food;
}
}
return nearestFood;
}
void EatFood(GameObject food)
{
energy += energyGain;
gameManager.RemoveFood(food);
Destroy(food);
}
void Reproduce()
{
energy /= 2; // 親のエネルギーを半分にする
gameManager.SendMessage("SpawnAnimal");
}
void Die()
{
gameManager.RemoveAnimal(gameObject);
Destroy(gameObject);
}
}
Food.cs(食料の管理)using UnityEngine;
public class Food : MonoBehaviour
{
void Start()
{
gameObject.tag = "Food";
}
}
GameManager.cs → ゲームの進行管理Animal.cs → エージェントのAI(移動・食事・繁殖・死亡)Food.cs → 食料の管理このコードをUnityに適用すれば、シンプルな生態系シミュレーションが動作します。
エージェントは食料を探して動き回り、エネルギー管理をしながら生き延びるようになります。
このシステムを拡張するには?
このコードをベースに、面白いシミュレーションを作ってみてください!🚀