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