(シールドの作成)シールドの外観を作る。ON・OFFを切り替える。





シールドのオン・オフを切り替える
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using UnityEngine.UI;
using TMPro;
public class PlayerHealth : MonoBehaviourPunCallbacks
{
private int maxHP = 10;
private int HP;
public Slider hpSlider;
private TextMeshProUGUI koLabel;
// ★追加
public GameObject shield;
private int num = 1;
void Start()
{
HP = maxHP;
hpSlider.maxValue = HP;
hpSlider.value = HP;
koLabel = GameObject.Find("KOLabel").GetComponent<TextMeshProUGUI>();
// ★追加
// ゲーム開始時はシールドオフ
shield.SetActive(false);
}
// ★追加
private void Update()
{
if (photonView.IsMine)
{
if (Input.GetKeyDown(KeyCode.Q))
{
// テクニック
// ボタンを押すごとにnumの変数が「-1」と「1」で交互に変化する。
num *= -1;
if (num == -1)
{
shield.SetActive(true);
}
else if (num == 1)
{
shield.SetActive(false);
}
}
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Trigger"))
{
HP -= 1;
print(photonView.Owner.NickName + HP);
hpSlider.value = HP;
if (HP < 1)
{
this.gameObject.SetActive(false);
koLabel.text = photonView.Owner.NickName + " KO!";
}
photonView.RPC("Damage", RpcTarget.Others);
}
}
// RPC→リモート・プロシージャ・コールの略称
// 相手に「ここで定義したメソッド」を「実行」してもらう仕組み
[PunRPC]
void Damage()
{
HP -= 1;
print(photonView.Owner.NickName + HP);
hpSlider.value = HP;
if (HP < 1)
{
this.gameObject.SetActive(false);
koLabel.text = photonView.Owner.NickName + " KO!";
}
}
}


【2021版】BattleOnline(全37回)
他のコースを見る




シールドのオン・オフを切り替える
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using UnityEngine.UI;
using TMPro;
public class PlayerHealth : MonoBehaviourPunCallbacks
{
private int maxHP = 10;
private int HP;
public Slider hpSlider;
private TextMeshProUGUI koLabel;
// ★追加
public GameObject shield;
private int num = 1;
void Start()
{
HP = maxHP;
hpSlider.maxValue = HP;
hpSlider.value = HP;
koLabel = GameObject.Find("KOLabel").GetComponent<TextMeshProUGUI>();
// ★追加
// ゲーム開始時はシールドオフ
shield.SetActive(false);
}
// ★追加
private void Update()
{
if (photonView.IsMine)
{
if (Input.GetKeyDown(KeyCode.Q))
{
// テクニック
// ボタンを押すごとにnumの変数が「-1」と「1」で交互に変化する。
num *= -1;
if (num == -1)
{
shield.SetActive(true);
}
else if (num == 1)
{
shield.SetActive(false);
}
}
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Trigger"))
{
HP -= 1;
print(photonView.Owner.NickName + HP);
hpSlider.value = HP;
if (HP < 1)
{
this.gameObject.SetActive(false);
koLabel.text = photonView.Owner.NickName + " KO!";
}
photonView.RPC("Damage", RpcTarget.Others);
}
}
// RPC→リモート・プロシージャ・コールの略称
// 相手に「ここで定義したメソッド」を「実行」してもらう仕組み
[PunRPC]
void Damage()
{
HP -= 1;
print(photonView.Owner.NickName + HP);
hpSlider.value = HP;
if (HP < 1)
{
this.gameObject.SetActive(false);
koLabel.text = photonView.Owner.NickName + " KO!";
}
}
}


(シールドの作成)シールドの外観を作る。ON・OFFを切り替える。