オブジェクトに耐久力(HP)を加える

オブジェクトに耐久力(HP)を加える
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyObject : MonoBehaviour
{
[SerializeField]
private GameObject effectPrefab;
// ★★追加
[SerializeField]
private GameObject effectPrefab2; // 2種類目のエフェクトを入れるための箱
public int objectHP;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Shell"))
{
// ★★追加
// オブジェクトのHPを1ずつ減少させる。
objectHP -= 1;
// ★★追加
// もしもHPが0よりも大きい場合には(条件)
if (objectHP > 0)
{
Destroy(other.gameObject);
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 2.0f);
}
else // ★★追加 そうでない場合(HPが0以下になった場合)には(条件)
{
Destroy(other.gameObject);
// もう1種類のエフェクを発生させる。
GameObject effect2 = Instantiate(effectPrefab2, transform.position, Quaternion.identity);
Destroy(effect2, 2.0f);
Destroy(this.gameObject);
}
}
}
}




被弾した場所にエフェクトを出す
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyObject : MonoBehaviour
{
[SerializeField]
private GameObject effectPrefab;
[SerializeField]
private GameObject effectPrefab2;
public int objectHP;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Shell"))
{
objectHP -= 1;
if (objectHP > 0)
{
Destroy(other.gameObject);
// ★改良
// otherを追加する。
GameObject effect = Instantiate(effectPrefab, other.transform.position, Quaternion.identity);
Destroy(effect, 2.0f);
}
else
{
Destroy(other.gameObject);
// ★改良
// otherを追加する。
GameObject effect2 = Instantiate(effectPrefab2, other.transform.position, Quaternion.identity);
Destroy(effect2, 2.0f);
Destroy(this.gameObject);
}
}
}
}

【2019版】BattleTank(基礎/全38回)
他のコースを見る
オブジェクトに耐久力(HP)を加える
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyObject : MonoBehaviour
{
[SerializeField]
private GameObject effectPrefab;
// ★★追加
[SerializeField]
private GameObject effectPrefab2; // 2種類目のエフェクトを入れるための箱
public int objectHP;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Shell"))
{
// ★★追加
// オブジェクトのHPを1ずつ減少させる。
objectHP -= 1;
// ★★追加
// もしもHPが0よりも大きい場合には(条件)
if (objectHP > 0)
{
Destroy(other.gameObject);
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 2.0f);
}
else // ★★追加 そうでない場合(HPが0以下になった場合)には(条件)
{
Destroy(other.gameObject);
// もう1種類のエフェクを発生させる。
GameObject effect2 = Instantiate(effectPrefab2, transform.position, Quaternion.identity);
Destroy(effect2, 2.0f);
Destroy(this.gameObject);
}
}
}
}




被弾した場所にエフェクトを出す
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyObject : MonoBehaviour
{
[SerializeField]
private GameObject effectPrefab;
[SerializeField]
private GameObject effectPrefab2;
public int objectHP;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Shell"))
{
objectHP -= 1;
if (objectHP > 0)
{
Destroy(other.gameObject);
// ★改良
// otherを追加する。
GameObject effect = Instantiate(effectPrefab, other.transform.position, Quaternion.identity);
Destroy(effect, 2.0f);
}
else
{
Destroy(other.gameObject);
// ★改良
// otherを追加する。
GameObject effect2 = Instantiate(effectPrefab2, other.transform.position, Quaternion.identity);
Destroy(effect2, 2.0f);
Destroy(this.gameObject);
}
}
}
}

オブジェクトに耐久力(HP)を加える