地雷を設置できるようにする




地雷の設置(オブジェクトプール)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SetMine : MonoBehaviour
{
public string playerName;
public GameObject minePrefab;
public AudioClip setSound;
public int maxValue;
private List<GameObject> mineList = new List<GameObject>();
// ★オブジェクトプーリングのテクニック
private void Start()
{
for(int i = 0; i < maxValue; i++)
{
// 最初に決めた分だけ地雷を作成
GameObject mine = Instantiate(minePrefab);
// 作成した地雷をいったんノンアクティブ状態にする。
mine.SetActive(false);
// 作成した地雷はリストに加えて管理する。
mineList.Add(mine);
}
}
void Update()
{
if (Input.GetButtonDown("Set" + playerName))
{
for (int i = 0; i < mineList.Count; i++)
{
// もしも○番目の地雷がノンアクティブ状態ならば・・・(条件)
if (!mineList[i].activeSelf)
{
// アクティブ状態にする。
mineList[i].SetActive(true);
// セット装置の位置に地雷を置く。
mineList[i].transform.position = this.gameObject.transform.position;
// 効果音を鳴らす。
AudioSource.PlayClipAtPoint(setSound, transform.position);
// いったん処理を抜ける。
break;
}
}
}
}
}



地雷をノンアクティブ状態に戻す
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Mine : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip sound;
private float power = 10f;
private float radius = 10f;
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Player" || collision.gameObject.tag == "Shell")
{
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
AudioSource.PlayClipAtPoint(sound, transform.position);
Vector3 explosionPos = transform.position;
Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
foreach(Collider hit in colliders)
{
Rigidbody rb = hit.GetComponent<Rigidbody>();
if(rb != null)
{
rb.AddExplosionForce(power, transform.position, radius, 1.0f, ForceMode.VelocityChange);
}
}
// ★★改良
//Destroy(gameObject);
this.gameObject.SetActive(false);
}
}
}












地雷の設置(オブジェクトプール)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SetMine : MonoBehaviour
{
public string playerName;
public GameObject minePrefab;
public AudioClip setSound;
public int maxValue;
private List<GameObject> mineList = new List<GameObject>();
// ★オブジェクトプーリングのテクニック
private void Start()
{
for(int i = 0; i < maxValue; i++)
{
// 最初に決めた分だけ地雷を作成
GameObject mine = Instantiate(minePrefab);
// 作成した地雷をいったんノンアクティブ状態にする。
mine.SetActive(false);
// 作成した地雷はリストに加えて管理する。
mineList.Add(mine);
}
}
void Update()
{
if (Input.GetButtonDown("Set" + playerName))
{
for (int i = 0; i < mineList.Count; i++)
{
// もしも○番目の地雷がノンアクティブ状態ならば・・・(条件)
if (!mineList[i].activeSelf)
{
// アクティブ状態にする。
mineList[i].SetActive(true);
// セット装置の位置に地雷を置く。
mineList[i].transform.position = this.gameObject.transform.position;
// 効果音を鳴らす。
AudioSource.PlayClipAtPoint(setSound, transform.position);
// いったん処理を抜ける。
break;
}
}
}
}
}



地雷をノンアクティブ状態に戻す
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Mine : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip sound;
private float power = 10f;
private float radius = 10f;
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Player" || collision.gameObject.tag == "Shell")
{
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
AudioSource.PlayClipAtPoint(sound, transform.position);
Vector3 explosionPos = transform.position;
Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
foreach(Collider hit in colliders)
{
Rigidbody rb = hit.GetComponent<Rigidbody>();
if(rb != null)
{
rb.AddExplosionForce(power, transform.position, radius, 1.0f, ForceMode.VelocityChange);
}
}
// ★★改良
//Destroy(gameObject);
this.gameObject.SetActive(false);
}
}
}








地雷を設置できるようにする