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

耐久力の追加
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyObject : MonoBehaviour
{
public GameObject effectPrefab;
// ★★追加
public 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
{
public GameObject effectPrefab;
public 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);
}
}
}
}

【2021版】BattleTank(基礎/全33回)
他のコースを見る
耐久力の追加
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyObject : MonoBehaviour
{
public GameObject effectPrefab;
// ★★追加
public 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
{
public GameObject effectPrefab;
public 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)を加える