敵を破壊するとアイテム出現
敵の破壊時にアイテムを出現させる
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, transform.position, Quaternion.identity);
Destroy(effect, 2.0f);
}
else
{
Destroy(other.gameObject);
GameObject effect2 = Instantiate(effectPrefab2, transform.position, Quaternion.identity);
Destroy(effect2, 2.0f);
Destroy(this.gameObject);
// ★追加
Instantiate(itemPrefab, transform.position, Quaternion.identity);
}
}
}
}
アイテムの出現場所を調整する
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, transform.position, Quaternion.identity);
Destroy(effect, 2.0f);
}
else
{
Destroy(other.gameObject);
GameObject effect2 = Instantiate(effectPrefab2, transform.position, Quaternion.identity);
Destroy(effect2, 2.0f);
Destroy(this.gameObject);
// ★改良
// アイテムの出現場所を調整する。(地面に埋まっているので、Y軸を調整する。)
Vector3 pos = transform.position;
Instantiate(itemPrefab, new Vector3(pos.x, pos.y + 0.5f, pos.z), Quaternion.identity);
}
}
}
}
BattleTank(基礎/全31回)
他のコースを見る敵の破壊時にアイテムを出現させる
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, transform.position, Quaternion.identity);
Destroy(effect, 2.0f);
}
else
{
Destroy(other.gameObject);
GameObject effect2 = Instantiate(effectPrefab2, transform.position, Quaternion.identity);
Destroy(effect2, 2.0f);
Destroy(this.gameObject);
// ★追加
Instantiate(itemPrefab, transform.position, Quaternion.identity);
}
}
}
}
アイテムの出現場所を調整する
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, transform.position, Quaternion.identity);
Destroy(effect, 2.0f);
}
else
{
Destroy(other.gameObject);
GameObject effect2 = Instantiate(effectPrefab2, transform.position, Quaternion.identity);
Destroy(effect2, 2.0f);
Destroy(this.gameObject);
// ★改良
// アイテムの出現場所を調整する。(地面に埋まっているので、Y軸を調整する。)
Vector3 pos = transform.position;
Instantiate(itemPrefab, new Vector3(pos.x, pos.y + 0.5f, pos.z), Quaternion.identity);
}
}
}
}
敵を破壊するとアイテム出現