ランダムイベントは、ゲームに変化をもたらします。
乱数生成と重み付け抽選で、バランスの取れたイベントが作れます。
この記事では、実装方法を詳しく解説します。
✨ この記事でわかること
- 乱数生成の基本
- 重み付け抽選の実装
- イベントテーブルの設計
- ガチャシステムへの応用
- 実装例とコード

ランダムイベントは、重み付け抽選から始めましょう。確率を調整することで、バランスの取れたイベントが作れます。
Unity入門の森を見る 初心者歓迎!動画×プロジェクト一式で本格ゲーム制作を学べる
あなたのオリジナルゲーム、今年こそ完成させませんか?
RPG・アクション・ホラー…Unityで本格ゲームを作りたい人のための学習サイトです。
実際に完成するゲームを題材に、
ソースコード・素材・プロジェクト一式をすべて公開。
仕事や学校の合間の1〜2時間でも、
「写経→改造」で自分のゲームまで作りきれる環境です。
乱数生成の基本

乱数生成は、ランダムイベントの基礎です。
Unityでの実装方法を紹介します。
基本的な乱数生成
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
using UnityEngine; public class RandomNumberGenerator : MonoBehaviour { void Start() { // 0から100までの乱数 int randomInt = Random.Range(0, 101); Debug.Log($"乱数(整数): {randomInt}"); // 0.0から1.0までの乱数 float randomFloat = Random.Range(0f, 1f); Debug.Log($"乱数(小数): {randomFloat}"); // 指定範囲の乱数 int randomInRange = Random.Range(10, 20); // 10以上20未満 Debug.Log($"範囲内の乱数: {randomInRange}"); } } |
このコードで、基本的な乱数生成が実装できます。
UnityのRandom.Rangeを使えば、簡単に乱数を生成できます。
確率判定の実装
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public class ProbabilityChecker : MonoBehaviour { public bool CheckProbability(float probability) { // 確率(0.0〜1.0)で判定 float random = Random.Range(0f, 1f); return random < probability; } void Example() { // 30%の確率で発生 if (CheckProbability(0.3f)) { Debug.Log("イベント発生!"); } // 50%の確率で発生 if (CheckProbability(0.5f)) { Debug.Log("50%のイベント発生!"); } } } |
このコードで、確率判定が実装できます。
パーセンテージを小数に変換して、判定します。
Unity入門の森を見る 初心者歓迎!動画×プロジェクト一式で本格ゲーム制作を学べる
重み付け抽選の実装

重み付け抽選は、確率を調整できます。
実装方法を紹介します。
重み付け抽選システム
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
[System.Serializable] public class WeightedItem { public string name; public int weight; // 重み } public class WeightedRandomSelector : MonoBehaviour { public List<WeightedItem> items = new List<WeightedItem>(); public WeightedItem SelectRandom() { // 重みの合計を計算 int totalWeight = 0; foreach (var item in items) { totalWeight += item.weight; } // ランダム値を生成 int randomValue = Random.Range(0, totalWeight); // 重みに応じて選択 int currentWeight = 0; foreach (var item in items) { currentWeight += item.weight; if (randomValue < currentWeight) { return item; } } // フォールバック return items[items.Count - 1]; } void Example() { // アイテムを設定 items.Add(new WeightedItem { name = "レアアイテム", weight = 1 }); items.Add(new WeightedItem { name = "ノーマルアイテム", weight = 9 }); // 抽選 WeightedItem selected = SelectRandom(); Debug.Log($"選択されたアイテム: {selected.name}"); } } |
このコードで、重み付け抽選が実装できます。
重みの合計に対して、ランダム値を比較して選択します。
✅ 重み付け抽選の例
- レアアイテム:重み1(10%の確率)
- ノーマルアイテム:重み9(90%の確率)
- 合計:重み10(100%)

重み付け抽選は、確率を調整しやすいのが利点です。重みを変更するだけで、確率を簡単に調整できます。
Unity入門の森を見る 初心者歓迎!動画×プロジェクト一式で本格ゲーム制作を学べる
イベントテーブルの設計

イベントテーブルは、イベントの種類を管理します。
設計方法を紹介します。
イベントテーブルシステム
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
[System.Serializable] public class GameEvent { public string eventID; public string eventName; public string description; public int weight; public EventType type; public EventEffect effect; } public enum EventType { Good, // 良いイベント Bad, // 悪いイベント Neutral // 中立イベント } [System.Serializable] public class EventEffect { public int moneyChange = 0; public int healthChange = 0; public int experienceChange = 0; } public class EventTable : MonoBehaviour { public List<GameEvent> events = new List<GameEvent>(); public GameEvent SelectRandomEvent() { // 重み付け抽選でイベントを選択 WeightedRandomSelector selector = GetComponent<WeightedRandomSelector>(); List<WeightedItem> weightedItems = new List<WeightedItem>(); foreach (var evt in events) { weightedItems.Add(new WeightedItem { name = evt.eventID, weight = evt.weight }); } selector.items = weightedItems; WeightedItem selected = selector.SelectRandom(); return events.Find(e => e.eventID == selected.name); } public void TriggerEvent(GameEvent evt) { Debug.Log($"イベント発生: {evt.eventName}"); Debug.Log($"説明: {evt.description}"); // 効果を適用 ApplyEffect(evt.effect); } void ApplyEffect(EventEffect effect) { // 効果を適用する処理 // 実装は省略 } } |
このコードで、イベントテーブルシステムが実装できます。
イベントの種類と効果を管理できます。
Unity入門の森を見る 初心者歓迎!動画×プロジェクト一式で本格ゲーム制作を学べる
ガチャシステムへの応用

ガチャシステムは、重み付け抽選の応用です。
実装方法を紹介します。
ガチャシステムの実装
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
[System.Serializable] public class GachaItem { public string itemID; public string itemName; public Rarity rarity; public int weight; } public enum Rarity { Common, // 通常(90%) Rare, // レア(8%) SuperRare, // 超レア(1.9%) UltraRare // 極レア(0.1%) } public class GachaSystem : MonoBehaviour { public List<GachaItem> items = new List<GachaItem>(); public GachaItem DrawGacha() { // レアリティごとに重みを設定 List<WeightedItem> rarityWeights = new List<WeightedItem>(); rarityWeights.Add(new WeightedItem { name = "Common", weight = 900 }); rarityWeights.Add(new WeightedItem { name = "Rare", weight = 80 }); rarityWeights.Add(new WeightedItem { name = "SuperRare", weight = 19 }); rarityWeights.Add(new WeightedItem { name = "UltraRare", weight = 1 }); // レアリティを抽選 WeightedRandomSelector selector = GetComponent<WeightedRandomSelector>(); selector.items = rarityWeights; WeightedItem selectedRarity = selector.SelectRandom(); // レアリティに応じたアイテムを抽選 Rarity rarity = (Rarity)System.Enum.Parse(typeof(Rarity), selectedRarity.name); List<GachaItem> rarityItems = items.FindAll(i => i.rarity == rarity); if (rarityItems.Count == 0) { return null; } // 同じレアリティ内でランダム選択 int randomIndex = Random.Range(0, rarityItems.Count); return rarityItems[randomIndex]; } } |
このコードで、ガチャシステムが実装できます。
レアリティごとに重みを設定し、2段階で抽選します。
Unity入門の森を見る 初心者歓迎!動画×プロジェクト一式で本格ゲーム制作を学べる
実装例:完全なランダムイベントシステム

実際に使える、完全なランダムイベントシステムの実装例を紹介します。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
using UnityEngine; public class CompleteRandomEventSystem : MonoBehaviour { public EventTable eventTable; public float eventInterval = 10f; // イベント発生間隔(秒) private float eventTimer = 0f; void Update() { eventTimer += Time.deltaTime; if (eventTimer >= eventInterval) { // ランダムイベントを発生 GameEvent evt = eventTable.SelectRandomEvent(); if (evt != null) { eventTable.TriggerEvent(evt); } eventTimer = 0f; } } public void TriggerEventManually() { // 手動でイベントを発生 GameEvent evt = eventTable.SelectRandomEvent(); if (evt != null) { eventTable.TriggerEvent(evt); } } } |
このコードで、完全なランダムイベントシステムが実装できます。
一定間隔で、または手動でイベントを発生させられます。
Unity入門の森を見る 初心者歓迎!動画×プロジェクト一式で本格ゲーム制作を学べる
よくある質問(FAQ)

Unity入門の森を見る 初心者歓迎!動画×プロジェクト一式で本格ゲーム制作を学べる
あなたのオリジナルゲーム、今年こそ完成させませんか?
RPG・アクション・ホラー…Unityで本格ゲームを作りたい人のための学習サイトです。
実際に完成するゲームを題材に、
ソースコード・素材・プロジェクト一式をすべて公開。
仕事や学校の合間の1〜2時間でも、
「写経→改造」で自分のゲームまで作りきれる環境です。
まとめ

ランダムイベントは、重み付け抽選から始めましょう。
確率を調整することで、バランスの取れたイベントが作れます。
✅ 今日から始める3ステップ
- ステップ1:重み付け抽選システムを実装する(所要2時間)
- ステップ2:イベントテーブルを設計する(所要1時間)
- ステップ3:ランダムイベントシステムを実装する(所要2時間)
本格的にUnityを学びたい方は、Unity入門の森で実践的なスキルを身につけましょう。
あなたのペースで、少しずつ進めていけば大丈夫です。
Unity入門の森を見る 初心者歓迎!動画×プロジェクト一式で本格ゲーム制作を学べる
あわせて読みたい(用途別)
- ランダムイベント生成の具体例(汎用)はこちら
→ Unity全ジャンルで使える、基本実装の例を確認できます。 - 経営ゲームに特化したイベント生成の実装はこちら
→ 経営シミュレーションゲーム向けの応用例を詳しく解説。 - ガチャシステムだけ知りたい方はこちら
→ ガチャ確率やレアリティ設定に特化した実装例を紹介。



コメント