(シールドの作成)シールドエネルギーを実装する
シールドエネルギーをスライダーで表示する
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;
// ★追加
public Slider shieldSlider;
private float maxEnergy = 100;
private float energy;
void Start()
{
HP = maxHP;
hpSlider.maxValue = HP;
hpSlider.value = HP;
koLabel = GameObject.Find("KOLabel").GetComponent<TextMeshProUGUI>();
shield.SetActive(false);
// ★追加
energy = maxEnergy;
shieldSlider.maxValue = energy;
shieldSlider.value = energy;
}
private void Update()
{
if (photonView.IsMine)
{
if (Input.GetKeyDown(KeyCode.Q))
{
num *= -1;
if (num == -1)
{
shield.SetActive(true);
}
else if (num == 1)
{
shield.SetActive(false);
}
photonView.RPC("ShieldManager", RpcTarget.Others);
}
// ★追加
if (num == -1) // シールドがオンの時
{
// エネルギー減少
energy -= Time.deltaTime * 5f;
// 下限の設定
if (energy < 0)
{
energy = 0;
}
}
else if (num == 1) // シールドがオフの時
{
// エネルギー回復
energy += Time.deltaTime * 5f;
// 上限の設定
if (energy > maxEnergy)
{
energy = maxEnergy;
}
}
shieldSlider.value = energy;
}
}
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);
}
}
[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!";
}
}
[PunRPC]
void ShieldManager()
{
num *= -1;
if (num == -1)
{
shield.SetActive(true);
}
else if (num == 1)
{
shield.SetActive(false);
}
}
}
【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;
// ★追加
public Slider shieldSlider;
private float maxEnergy = 100;
private float energy;
void Start()
{
HP = maxHP;
hpSlider.maxValue = HP;
hpSlider.value = HP;
koLabel = GameObject.Find("KOLabel").GetComponent<TextMeshProUGUI>();
shield.SetActive(false);
// ★追加
energy = maxEnergy;
shieldSlider.maxValue = energy;
shieldSlider.value = energy;
}
private void Update()
{
if (photonView.IsMine)
{
if (Input.GetKeyDown(KeyCode.Q))
{
num *= -1;
if (num == -1)
{
shield.SetActive(true);
}
else if (num == 1)
{
shield.SetActive(false);
}
photonView.RPC("ShieldManager", RpcTarget.Others);
}
// ★追加
if (num == -1) // シールドがオンの時
{
// エネルギー減少
energy -= Time.deltaTime * 5f;
// 下限の設定
if (energy < 0)
{
energy = 0;
}
}
else if (num == 1) // シールドがオフの時
{
// エネルギー回復
energy += Time.deltaTime * 5f;
// 上限の設定
if (energy > maxEnergy)
{
energy = maxEnergy;
}
}
shieldSlider.value = energy;
}
}
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);
}
}
[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!";
}
}
[PunRPC]
void ShieldManager()
{
num *= -1;
if (num == -1)
{
shield.SetActive(true);
}
else if (num == 1)
{
shield.SetActive(false);
}
}
}
(シールドの作成)シールドエネルギーを実装する