敵を破壊するとアイテム出現



アイテム出現
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyObject : MonoBehaviour
{
public GameObject effectPrefab;
public GameObject effectPrefab2;
public int objectHP;
// ★追加
public GameObject itemPrefab;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Shell"))
{
objectHP -= 1;
if (objectHP > 0)
{
Destroy(other.gameObject);
GameObject effect = Instantiate(effectPrefab, other.transform.position, Quaternion.identity);
Destroy(effect, 2.0f);
}
else
{
Destroy(other.gameObject);
GameObject effect2 = Instantiate(effectPrefab2, other.transform.position, Quaternion.identity);
Destroy(effect2, 2.0f);
Destroy(this.gameObject);
// ★追加
// (ポイント)pos.y + 0.6fのコードでアイテムの出現場所の「高さ」を調整しています。
Vector3 pos = transform.position;
Instantiate(itemPrefab, new Vector3(pos.x, pos.y + 0.6f, pos.z), Quaternion.identity);
}
}
}
}



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


アイテム出現
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyObject : MonoBehaviour
{
public GameObject effectPrefab;
public GameObject effectPrefab2;
public int objectHP;
// ★追加
public GameObject itemPrefab;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Shell"))
{
objectHP -= 1;
if (objectHP > 0)
{
Destroy(other.gameObject);
GameObject effect = Instantiate(effectPrefab, other.transform.position, Quaternion.identity);
Destroy(effect, 2.0f);
}
else
{
Destroy(other.gameObject);
GameObject effect2 = Instantiate(effectPrefab2, other.transform.position, Quaternion.identity);
Destroy(effect2, 2.0f);
Destroy(this.gameObject);
// ★追加
// (ポイント)pos.y + 0.6fのコードでアイテムの出現場所の「高さ」を調整しています。
Vector3 pos = transform.position;
Instantiate(itemPrefab, new Vector3(pos.x, pos.y + 0.6f, pos.z), Quaternion.identity);
}
}
}
}



敵を破壊するとアイテム出現