プレーヤーにHPをつける



プレーヤーHP
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TankHealth : MonoBehaviour
{
public GameObject effectPrefab1;
public GameObject effectPrefab2;
public int tankHP;
private void OnTriggerEnter(Collider other)
{
// もしもぶつかってきた相手のTagが”EnemyShell”であったならば(条件)
if (other.CompareTag("EnemyShell"))
{
// HPを1ずつ減少させる。
tankHP -= 1;
// ぶつかってきた相手方(敵の砲弾)を破壊する。
Destroy(other.gameObject);
if (tankHP > 0)
{
GameObject effect1 = Instantiate(effectPrefab1, transform.position, Quaternion.identity);
Destroy(effect1, 1.0f);
}
else // 上の条件が成立しなくなった場合にはこちらを実行する。
{
GameObject effect2 = Instantiate(effectPrefab2, transform.position, Quaternion.identity);
Destroy(effect2, 1.0f);
// プレーヤーを破壊する。
Destroy(gameObject);
}
}
}
}




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


プレーヤーHP
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TankHealth : MonoBehaviour
{
public GameObject effectPrefab1;
public GameObject effectPrefab2;
public int tankHP;
private void OnTriggerEnter(Collider other)
{
// もしもぶつかってきた相手のTagが”EnemyShell”であったならば(条件)
if (other.CompareTag("EnemyShell"))
{
// HPを1ずつ減少させる。
tankHP -= 1;
// ぶつかってきた相手方(敵の砲弾)を破壊する。
Destroy(other.gameObject);
if (tankHP > 0)
{
GameObject effect1 = Instantiate(effectPrefab1, transform.position, Quaternion.identity);
Destroy(effect1, 1.0f);
}
else // 上の条件が成立しなくなった場合にはこちらを実行する。
{
GameObject effect2 = Instantiate(effectPrefab2, transform.position, Quaternion.identity);
Destroy(effect2, 1.0f);
// プレーヤーを破壊する。
Destroy(gameObject);
}
}
}
}




プレーヤーにHPをつける