相手が発射した砲弾でもダメージを受けるようにする
条件文の「または」の使い方
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class TankHealth : MonoBehaviour
{
public GameObject effectPrefab1;
public GameObject effectPrefab2;
public int tankHP;
public Text HPLabel;
void Start()
{
HPLabel.text = "HP:" + tankHP;
}
private void OnTriggerEnter(Collider other)
{
// ★追加
// (重要ポイント)「||」の意味は「または」
// 「条件A または 条件Bのどちらかが成立したならば・・・・、実行する」
if (other.gameObject.tag == "EnemyShell" || other.gameObject.tag == "Shell")
{
tankHP -= 1;
HPLabel.text = "HP:" + tankHP;
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);
this.gameObject.SetActive(false);
Invoke("GoToGameOver", 1.5f);
}
}
}
void GoToGameOver()
{
SceneManager.LoadScene("GameOver");
}
public void AddHP(int amount)
{
tankHP += amount;
if (tankHP > 3)
{
tankHP = 3;
}
HPLabel.text = "HP:" + tankHP;
}
}
条件文の「または」の使い方
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class TankHealth : MonoBehaviour
{
public GameObject effectPrefab1;
public GameObject effectPrefab2;
public int tankHP;
public Text HPLabel;
void Start()
{
HPLabel.text = "HP:" + tankHP;
}
private void OnTriggerEnter(Collider other)
{
// ★追加
// (重要ポイント)「||」の意味は「または」
// 「条件A または 条件Bのどちらかが成立したならば・・・・、実行する」
if (other.gameObject.tag == "EnemyShell" || other.gameObject.tag == "Shell")
{
tankHP -= 1;
HPLabel.text = "HP:" + tankHP;
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);
this.gameObject.SetActive(false);
Invoke("GoToGameOver", 1.5f);
}
}
}
void GoToGameOver()
{
SceneManager.LoadScene("GameOver");
}
public void AddHP(int amount)
{
tankHP += amount;
if (tankHP > 3)
{
tankHP = 3;
}
HPLabel.text = "HP:" + tankHP;
}
}
相手が発射した砲弾でもダメージを受けるようにする