アイテムのランダム出現(Addressable Asset System)







アイテムのランダム出現(Addressables)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★追加
using UnityEngine.AddressableAssets;
public abstract class EnemyBase : MonoBehaviour
{
public int HP;
public int ScoreValue;
public virtual void TakeDamage(int missilePower)
{
HP -= missilePower;
if (HP < 1)
{
Destroy(gameObject);
GameObject.Find("ScoreManager").GetComponent<ScoreManager>().AddScore(ScoreValue);
// ★追加
int num = Random.Range(0, 100); // 0〜99までの数字をランダムに選択
// ★追加
// if文でアイテムごとの出現確率をコントロール(確率は自由に設定)
// 自分のアイテム数に応じて条件を追加すること。
if (num >= 95) // 出現確率5%
{
// 各アイテムのパスをコピー&ペーストする。
Addressables.InstantiateAsync("Assets/MyPrefabs/MoveSpeedUp.prefab", transform.position, Quaternion.identity);
}
else if (num >= 90) // 出現確率5%
{
Addressables.InstantiateAsync("Assets/MyPrefabs/ShotSpeedUp.prefab", transform.position, Quaternion.identity);
}
else if (num >= 80) // 出現確率10%
{
Addressables.InstantiateAsync("Assets/MyPrefabs/RapidFire.prefab", transform.position, Quaternion.identity);
}
// 今回は80%の確率で「スカ」(何もアイテムが出現しない)にしています(スカの確率も自由です)
}
}
}

【2021版】Danmaku(基礎/全55回)
他のコースを見る






アイテムのランダム出現(Addressables)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★追加
using UnityEngine.AddressableAssets;
public abstract class EnemyBase : MonoBehaviour
{
public int HP;
public int ScoreValue;
public virtual void TakeDamage(int missilePower)
{
HP -= missilePower;
if (HP < 1)
{
Destroy(gameObject);
GameObject.Find("ScoreManager").GetComponent<ScoreManager>().AddScore(ScoreValue);
// ★追加
int num = Random.Range(0, 100); // 0〜99までの数字をランダムに選択
// ★追加
// if文でアイテムごとの出現確率をコントロール(確率は自由に設定)
// 自分のアイテム数に応じて条件を追加すること。
if (num >= 95) // 出現確率5%
{
// 各アイテムのパスをコピー&ペーストする。
Addressables.InstantiateAsync("Assets/MyPrefabs/MoveSpeedUp.prefab", transform.position, Quaternion.identity);
}
else if (num >= 90) // 出現確率5%
{
Addressables.InstantiateAsync("Assets/MyPrefabs/ShotSpeedUp.prefab", transform.position, Quaternion.identity);
}
else if (num >= 80) // 出現確率10%
{
Addressables.InstantiateAsync("Assets/MyPrefabs/RapidFire.prefab", transform.position, Quaternion.identity);
}
// 今回は80%の確率で「スカ」(何もアイテムが出現しない)にしています(スカの確率も自由です)
}
}
}

アイテムのランダム出現(Addressable Asset System)