(シールドの作成)スライダーの増減(動き)を同期する
スライダーの増減の同期
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);
}
}
// ★変更(下記のコードをphotonView.IsMineの「外側に」移動する。)
// ポイント
// 相手の画面(すなわち、photonView.IsMineではない場所)でenergyの数値を動かすため
// ここから
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);
}
}
// ★変更(下記のコードをphotonView.IsMineの「外側に」移動する。)
// ポイント
// 相手の画面(すなわち、photonView.IsMineではない場所)でenergyの数値を動かすため
// ここから
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);
}
}
}
(シールドの作成)スライダーの増減(動き)を同期する