ãUnity9ãUNETã§æ»ãã Playerãçãè¿ãããï¼ãUNET11ã
Unity5.1.1p3 Personal(2015å¹´7æ)
前回の続きã§ãã
ååã¯Playerã®HPã0ã«ãªã£ãæãPlayerãã²ã¼ã ç»é¢ä¸ããæ¶ãã¨ããã¾ã§ããã¾ããã
ä»åã¯ç»é¢ä¸ã«åºç¾ãããã¿ã³ãæ¼ããPlayerãçãè¿ãããã¨ããã¾ã§é²ãã¾ãã
UNET Part 11 - Player Resurrection - YouTube
ãç®æ¨ãæ»ãã Playerãçãè¿ãããï¼
â Player_Healthã¹ã¯ãªããå çä¿®æ£
Player_Healthã¹ã¯ãªããã«å°ãå çãã¦ããã¾ãã
using UnityEngine; using System.Collections; using UnityEngine.Networking; using UnityEngine.UI; public class Player_Health : NetworkBehaviour { [SyncVar (hook = "OnHealthChanged")] private int health = 100; private Text healthText; private bool shouldDie = false; public bool isDead = false; public delegate void DieDelegate(); public event DieDelegate EventDie; //********** éå§ **********// //Playeråçæã®ããã®event public delegate void RespawnDelegate(); public event RespawnDelegate EventRespawn; //********** çµäº **********// void Start () { healthText = GameObject.Find("Health Text").GetComponent<Text>(); SetHealthText(); } void Update () { CheckCondition(); } void CheckCondition () { if (health <= 0 && !shouldDie && !isDead) { shouldDie = true; } if (health <= 0 && shouldDie) { if (EventDie != null) { EventDie (); } shouldDie = false; } //********** éå§ **********// //HPã1以ä¸ããã®ã«isDead=trueã®æ if (health > 0 && isDead){ //EventRespawnã«ä½ãç»é²ããã¦ããæ if (EventRespawn != null){ //EventRespawnå®è¡ EventRespawn(); } isDead = false; } //********** çµäº **********// } void SetHealthText () { if (isLocalPlayer) { healthText.text = "Health " + health.ToString(); } } public void DeductHealth(int dmg) { health -= dmg; } void OnHealthChanged (int hlth) { health = hlth; SetHealthText(); } //********** éå§ **********// public void ResetHealth () { //Player_Respawnã¹ã¯ãªããã®CmdRespawnOnServerã¡ã½ããã[Command]ã®ããã //SyncVarãæ©è½ãã health = 100; } //********** çµäº **********// }
Player_Health.cs
EventRespawnã¨ããeventãä½æããhealthã1以ä¸ããã®ã«isDeadãtrueã®æã«å®è¡ããããã«ãã¾ããã
ã¾ããhealthã100ã«å復ããResetHealthã¨ããã¡ã½ãããä½æãã¾ããã
â¡çãè¿ãããããã®ãã¿ã³ãä½æ
ç¶ãã¦Player復活ç¨ã®ãã¿ã³ãä½æãã¦ããã¾ããMainã·ã¼ã³ã§Hierarchy > Create > UI > Buttonãé¸æããååããRespawn Buttonãã«ãã¦ä¸ããã
ã¾ããButtonã®Textã¯ãRespawn!ãã«å¤æ´ãã¦ä¸ããã
Buttonã¯éã¢ã¯ãã£ãåãã¦ããã¾ãã
ãã§ãã¯ãå¤ãã¦éã¢ã¯ãã£ãå
éã¢ã¯ãã£ãåããUIãã¼ããã¢ã¯ãã£ãåããã«ã¯ãFindã¡ã½ããã使ããªãã£ããã§ãªããªã大å¤ã§ãããªã®ã§ã対çããã¦ããã¾ãã
ã¾ãã¯ç©ºGameObjectãä½æãã¾ããååã¯ãGameManagerãã«ãã¾ãã
GameManager
GameManagerã«ä¸ã®ã¹ã¯ãªãã(GameManager_References)ãä»ãã¾ãã
using UnityEngine; using System.Collections; public class GameManager_References : MonoBehaviour { public GameObject respawnButton; }
GameManager_References.cs
ä½æããããpublicå¤æ°ã®respawnButtonã«ãRespawn Buttonãªãã¸ã§ã¯ããæå®ãã¾ãã
ãã©ãã°ï¼ããããï¼
ããã§Buttonãªãã¸ã§ã¯ããéã¢ã¯ãã£ãåãã¦ãå¼ã³åºããããããã«ãªãã¾ãããï¼å¾è¿°ï¼
â¢Player_Respawnã¹ã¯ãªããä½æ
ããã§ã¯ç¶ãã¦ãPlayer_Respawnã¹ã¯ãªãããæ°è¦ä½æãã¦ãPlayerãã¬ããã«åãä»ãã¦ä¸ããã
Player_Deathã¹ã¯ãªããã§Playerã®å§¿ãæ¶ãã¾ãããããã®éããã£ã¦ããæãã§ãã
以ä¸ã½ã¼ã¹ã§ãã
using UnityEngine; using System.Collections; using UnityEngine.Networking; using UnityEngine.UI; public class Player_Respawn : NetworkBehaviour { //Player_Healthã¹ã¯ãªãã private Player_Health healthScript; //ç §æºç»å private Image crossHairImage; //Respawnãã¿ã³ private GameObject respawnButton; void Start () { //Player_Healthã¹ã¯ãªããããã£ãã·ã¥ healthScript = GetComponent<Player_Health>(); //eventãç»é² healthScript.EventRespawn += EnablePlayer; //ç §æºç»åããã£ãã·ã¥ crossHairImage = GameObject.Find("Crosshair Image").GetComponent<Image>(); SetRespawnButton(); } void SetRespawnButton () { if (isLocalPlayer) { //GameManagerãçµç±ãã¦RespawnButtonãåå¾ respawnButton = GameObject.Find("GameManager").GetComponent<GameManager_References>().respawnButton; //onClick.AddListener: ãã¿ã³ãã¯ãªãã¯ãããæã«ã¡ã½ãããå®è¡ãã respawnButton.GetComponent<Button>().onClick.AddListener(CommenceRespawn); //ãã¿ã³ãæ¶ã respawnButton.SetActive(false); } } void Update () { } //ã¡ã¢ãªã¼ãªã¼ã¯çã®ä¸å ·åãèµ·ããæã®å¯¾ç void OnDisable() { healthScript.EventRespawn -= EnablePlayer; } //Player_Deathã¹ã¯ãªããã®DisablePlayerã¡ã½ããã®falseãtrueã«å¤ããæã //(health > 0 && isDead)ã®æã«å®è¡ããã void EnablePlayer() { GetComponent<CharacterController> ().enabled = true; GetComponent<Player_Shooting> ().enabled = true; GetComponent<BoxCollider> ().enabled = true; Renderer[] renderers = GetComponentsInChildren<Renderer> (); foreach (Renderer ren in renderers) { ren.enabled = true; } if(isLocalPlayer){ GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().enabled = true; crossHairImage.enabled = true; //Respawnãã¿ã³ãæ¶ã(åç»ã®äººãå¾ã§ä¿®æ£ããç®æï¼ respawnButton.SetActive(false); } } //Respawnãã¿ã³ãæ¼ããæã«å®è¡ void CommenceRespawn() { CmdRespawnOnServer(); } [Command] void CmdRespawnOnServer() { //healthã100ã«æ»ããSyncVarãæ©è½ããã healthScript.ResetHealth(); } }
Player_Respawn.cs
Startã¡ã½ããã«ã¦EnablePlayerã¡ã½ãããeventã¨ãã¦ç»é²ãã¦ãã¾ããPlayer_Healthã¹ã¯ãªããå
ã§ã(health > 0 && isDead)ã®æ¡ä»¶ãæºããããæã«å®è¡ãããeventã§ããã
ã¾ããStartã¡ã½ããæå¾ã§SetRespawnButtonã¡ã½ãããå¼ã°ãã¦ãã¾ãã
SetRespawnButtonã¡ã½ããã§ã¯ãå ã»ã©ä½æããGameManagerãçµç±ãã¦Respawnãã¿ã³ãåå¾ãã¦ãã¾ãã
ç¶ãã¦onClick.AddListenerã§ããã¿ã³ãæ¼ãããæã«CommenceRespawnã¡ã½ãããå®è¡ãããããç»é²ãã¦ãã¾ãã
ããã©ã«ããéã¢ã¯ãã£ãç¶æ ãªã®ã§å¤§ä¸å¤«ã ã¨æãã¾ãããä¸å¿ãã¿ã³ãæ¶ãã¦ãã¾ãã
OnDisableã¡ã½ããã¯Player_Deathã¡ã½ããã¨åãã§ã念ã®ããã¨ãã£ãæãã§è¨è¿°ãã¦ãã¾ãã
EnablePlayerã¡ã½ããã§ãããPlayer_Deathã¹ã¯ãªããã®DisablePlayerã¡ã½ããã¨ã»ã¨ãã©çéã®å 容ã§ãã
isDeadã¯Player_Healthã¹ã¯ãªããã§å¤æ´ãã¦ããã®ã§ããã¡ãã«ã¯ããã¾ããã
æå¾ã«Player_Deathã¹ã¯ãªããã«ä¸è¡è¿½å ãã¦çµããã§ãã以ä¸ã½ã¼ã¹ã§ãã
void DisablePlayer () { GetComponent<CharacterController> ().enabled = false; GetComponent<Player_Shooting> ().enabled = false; GetComponent<BoxCollider> ().enabled = false; Renderer[] renderers = GetComponentsInChildren<Renderer> (); foreach (Renderer ren in renderers) { ren.enabled = false; } healthScript.isDead = true; if(isLocalPlayer){ GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().enabled = false; crossHairImage.enabled = false; //********** éå§ **********// //Respawnãã¿ã³ãã¢ã¯ãã£ãå GameObject.Find("GameManager").GetComponent<GameManager_References>().respawnButton.SetActive(true); //********** çµäº **********// } }
Player_Death.cs
ä¸çªæå¾ã®è¡ã«Respawnãã¿ã³ãã¢ã¯ãã£ãåããè¨è¿°ã追å ããã ãã§ãã
â£çµæ
ããã§ã¯ã²ã¼ã ãå®è¡ãã¦ã¿ã¾ãããã
殺ãããæã«Respawnãã¿ã³ãåºç¾
ãã¿ã³ãæ¼ãã¨healthãå復ãã復活
ãã¾ãããã¾ããã
åç»ã®æå¾ã§ã¾ã³ããããã¨ããè¨èãèãããã®ã§ã次åã¯æµãã£ã©ãåºã¦ããæãã§ãã
ããã§ã¯ãä»åã¯ããã¾ã§ã§ãã
ãããã¨ããããã¾ããã
ãUnity9ãUNETã§ãã«ããã¬ã¤ã¤ã¼ãªãªã³ã©ã¤ã³ã²ã¼ã éçºãUNET1ã
ãUnity9ãUNETã§ãã¬ã¤ã¤ã¼ã®åããè£éãããªããããªåããå®ç¾ãããUNET1-2ã
ãUnity9ãUNETã§ãããã¯ã¼ã¯è¶ãã«å¾ã(Rotation)ãåæããããUNET2ã
ãUnity9ãUNETã§ãªã³ã©ã¤ã³éå§æã®ãã¬ã¤ã¤ã¼çæä½ç½®ãå¤æ´ãããUNET2-2ã
ãUnity9ãUNETã®ãããã¯ã¼ã¯ãã©ãã£ãã¯ã軽æ¸ããå¹çåãããUNET3ã
ãUnity9ãUNETã®latency(é
延æé)ãæ¹åãããUNET4ã
ãUnity9ãUNETã®latency(é
延æé)ã表示ãã¦ãã¡ãã£ã¨ã ãæ¹åãããUNET4-2ã
ãUnity9ãUNETã®SyncVarã®hookã®ä½¿ç¨ä¾ã¨ãåæ代ã®åææ¹æ³ãUNET5ã
ãUnity9ãUNETã使ã£ã¦RotationãåæããããUNET6ã
ãUnity9ãUNETã§åãã¬ã¤ã¤ã¼ã«PlayerIDãè¨å®ãããUNET7ã
ãUnity9ãUNETã§æµãã¬ã¤ã¤ã¼ã«ãã¡ã¼ã¸ãä¸ããï¼ãUNET8ã
ãUnity9ãUNETã§HPãç»é¢ã«è¡¨ç¤ºããPlayerã¸ãã¡ã¼ã¸ãä¸ãããUNET9ã
ãUnity9ãUNETã§HPã0以ä¸ã«ãªã£ãæãPlayerãç ´å£ããï¼ãUNET10ã
ãUnity9ãUNETã§æ»ãã Playerãçãè¿ãããï¼ãUNET11ã
ãUnity9ãUNETã§ã¾ã³ãAIãåºç¾ãããï¼ãUNET12ã
ãUnity9ãUNETã§ã¾ã³ãçææã«ã¦ãã¼ã¯ãªIDãä»ããï¼ãUNET13ã
ãUnity9ãUNETã§ã¾ã³ããæã¤ï¼ãUNET14ã
ãUnity9ãUNETã§ã¾ã³ãã«æ»æãããï¼ãUNET15ã
ãUnity9ãUNETã§ã¾ã³ãã®åããã¹ã ã¼ãºã«ã·ã³ã¯ããããï¼ãUNET16ã
ãUnity9ãUNETã§ã¾ã³ãçºçå°ç¹ãå¢ãããUNET17ã
ãUnity9ãUNETã§çºçãããã°ãåãé¤ããUNET18ã
ãUnity9ãUnity MultiPlayerã使ã£ã¦ãããã¯ã¼ã¯è¶ãã«ãããã¡ã¤ãã³ã°ï¼ãUNET19ã
ãUnity9ãGUIãæ¹åãã¦ãNetwork Managerãè¦ãããããï¼ãUNET20ã
ãUnity9ã表示ããGUI(Network Manager)ãæ©è½ãããï¼ãUNET20-2ã
ãUnity9ãUNETã§Animationãåæãããï¼ãUNET21ã