(シールドの作成)シールドエネルギーを実装する
シールドエネルギーをスライダーで表示する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using UnityEngine.UI;
public class PlayerHealth : MonoBehaviourPunCallbacks
{
private int maxHP = 10;
private int HP;
public Slider hpSlider;
private Text koLabel;
public GameObject shield;
private int sNum = 1;
// ★追加
public Slider shieldSlider;
private float maxEnergy = 100;
private float energy;
private void Start()
{
HP = maxHP;
hpSlider.maxValue = HP;
hpSlider.value = HP;
koLabel = GameObject.Find("KOLabel").GetComponent<Text>();
// ★追加
energy = maxEnergy;
shieldSlider.maxValue = energy;
shieldSlider.value = energy;
}
private void Update()
{
if(photonView.IsMine)
{
if(Input.GetKeyDown(KeyCode.S))
{
sNum *= -1;
if(sNum == -1)
{
shield.SetActive(true);
}
else if(sNum == 1)
{
shield.SetActive(false);
}
photonView.RPC("ShieldManager", RpcTarget.Others);
}
// ★追加
if(sNum == -1) // シールドがオンの時
{
energy -= Time.deltaTime * 5f;
// 下限の設定
if(energy < 0)
{
energy = 0;
}
}
else if(sNum == 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()
{
sNum *= -1;
if (sNum == -1)
{
shield.SetActive(true);
}
else if (sNum == 1)
{
shield.SetActive(false);
}
}
}
【2020版】BattleOnline(基礎/全34回)
他のコースを見るシールドエネルギーをスライダーで表示する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using UnityEngine.UI;
public class PlayerHealth : MonoBehaviourPunCallbacks
{
private int maxHP = 10;
private int HP;
public Slider hpSlider;
private Text koLabel;
public GameObject shield;
private int sNum = 1;
// ★追加
public Slider shieldSlider;
private float maxEnergy = 100;
private float energy;
private void Start()
{
HP = maxHP;
hpSlider.maxValue = HP;
hpSlider.value = HP;
koLabel = GameObject.Find("KOLabel").GetComponent<Text>();
// ★追加
energy = maxEnergy;
shieldSlider.maxValue = energy;
shieldSlider.value = energy;
}
private void Update()
{
if(photonView.IsMine)
{
if(Input.GetKeyDown(KeyCode.S))
{
sNum *= -1;
if(sNum == -1)
{
shield.SetActive(true);
}
else if(sNum == 1)
{
shield.SetActive(false);
}
photonView.RPC("ShieldManager", RpcTarget.Others);
}
// ★追加
if(sNum == -1) // シールドがオンの時
{
energy -= Time.deltaTime * 5f;
// 下限の設定
if(energy < 0)
{
energy = 0;
}
}
else if(sNum == 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()
{
sNum *= -1;
if (sNum == -1)
{
shield.SetActive(true);
}
else if (sNum == 1)
{
shield.SetActive(false);
}
}
}
(シールドの作成)シールドエネルギーを実装する