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



敵を破壊するとアイテム出現
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyObject : MonoBehaviour
{
[SerializeField]
private GameObject effectPrefab;
[SerializeField]
private GameObject effectPrefab2;
public int objectHP;
// ★追加
[SerializeField]
private 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);
}
}
}
}


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


敵を破壊するとアイテム出現
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyObject : MonoBehaviour
{
[SerializeField]
private GameObject effectPrefab;
[SerializeField]
private GameObject effectPrefab2;
public int objectHP;
// ★追加
[SerializeField]
private 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);
}
}
}
}


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