(スクリプト)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomShot : MonoBehaviour
{
public GameObject[] missileBox;
public AudioClip sound;
private int num;
private GameObject missile;
void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
num = Random.Range(0, 100);
if(num < 3) // 確率3%・・・>黄金弾
{
missile = Instantiate(missileBox[0], transform.position, Quaternion.identity);
}
else if(num < 13) // 確率10%・・・>銀弾
{
missile = Instantiate(missileBox[1], transform.position, Quaternion.identity);
}
else if (num < 40) // 確率27%・・・>銅弾
{
missile = Instantiate(missileBox[2], transform.position, Quaternion.identity);
}
else // 確率60%・・・>ノーマル弾
{
missile = Instantiate(missileBox[3], transform.position, Quaternion.identity);
}
Rigidbody missileRb = missile.GetComponent<Rigidbody>();
missileRb.AddForce(transform.forward * 500);
AudioSource.PlayClipAtPoint(sound, transform.position);
Destroy(missile, 3.0f);
}
}
}
(実行結果)