ãUnity9ãUNETã§HPã0以ä¸ã«ãªã£ãæãPlayerãç ´å£ããï¼ãUNET10ã
Unity5.1.1p3 Personal(2015å¹´7æ)
前回の続きã§ãã
ä»åã¯Playerã®HPã0以ä¸ã«ãªã£ãæã«ãPlayerãªãã¸ã§ã¯ããç ´å£ããå¦çãä½ã£ã¦ããã¾ãã
UNET Part 10 - Player Destruction! - YouTube
ãç®æ¨ãHPã0以ä¸ã«ãªã£ãæPlayerãç ´å£ããï¼
â Player_Healthã¹ã¯ãªãããä¿®æ£
Playerãç ´å£ããã¨ãã£ã¦ãå®éã¯ã²ã¼ã ç»é¢ããã¯åé¤ããã«ã移åæ段ãRendererçãéã¢ã¯ãã£ãåãã¦ç ´å£ããããã®ããã«è¦ããããææ³ã使ãã¾ãã
ããã§ã¯ã¾ãã¯ãååã¾ã§ã«ä½æãã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; //æ»ãã æã«æ©è½ããDelegateã¨Event //Event: ã¡ã½ãããç»é²ãã¦ãããä»»æã®ã¿ã¤ãã³ã°ã§å¼ã³åºã public delegate void DieDelegate(); public event DieDelegate EventDie; //********** çµäº **********// void Start () { healthText = GameObject.Find("Health Text").GetComponent<Text>(); SetHealthText(); } void Update () { //********** éå§ **********// CheckCondition(); //********** çµäº **********// } //********** éå§ **********// void CheckCondition () { //HPã0ã«ãªã£ãæ if (health <= 0 && !shouldDie && !isDead) { shouldDie = true; } //HPã0ã§ãshouldDieãtrueã®æ if (health <= 0 && shouldDie) { //Eventãç»é²ããã¦ããæ if (EventDie != null) { //Eventå®è¡ EventDie(); } shouldDie = false; } } //********** çµäº **********// void SetHealthText () { if (isLocalPlayer) { healthText.text = "Health " + health.ToString(); } } public void DeductHealth(int dmg) { health -= dmg; } void OnHealthChanged (int hlth) { health = hlth; SetHealthText(); } }
Player_Health.cs
ãããªãdelegateã»eventã¨è¦æ £ããªãè¨èãåºã¦ãã¾ããããã¡ããã©ã®ã¿ã¤ãã³ã°ã§デリゲートとは【C#】ã§è§£èª¬ããã¦ãã¾ããã®ã§ãåèã«ããã¦é ãã¾ãã
DelegateãEventãã¡ã½ãããå¤æ°ã«ç»é²ãã¦ããæ©è½ãæã£ã¦ããããã§ãä»åç¹ã«ä½¿ãEventã¯Delegateã®ä¸ç¨®ã§ã
ä»ã®ã¯ã©ã¹ã«ã¡ã½ãããç»é²ãã¨ãã¦ããããç¹å®ã®ã¿ã¤ãã³ã°ã§ãããå®è¡ãã
ã¨ããç¹å¾´ãããããã§ãã
ä»åã®ä¾ã§ã¯å¾ã§åºã¦ããPlayer_Deathã¯ã©ã¹ã§ã¡ã½ãããç»é²ãã¦ããã¦ããããPlayer_Healthã¯ã©ã¹ã®CheckConditionã¡ã½ããå ã§Eventãå®è¡ããæãã§ãã
ã©ã解説ãããã®ãã¨æã£ã¦ããã®ã§ããåããããã解説è¨äºããã大å¤å©ããã¾ãããkan_kikuchiããã大å¤ãããã¨ããããã¾ããã
ããã§ã¯ã¹ã¯ãªããã®è§£èª¬ã«æ»ãã¾ãã
ã¾ãã¯Updateã¡ã½ããããCheckConditionã¡ã½ãããå¼ã³ã¾ãã
CheckConditionã¡ã½ããã§ã¯ãhealthã0以ä¸ã«ãªãshouldDieã¨isDeadãfalseã®æã«ãshouldDieãtrueã«ãã¾ãã
shouldDieãtrueã«ãªãã¨æ¬¡ã®healthã0以ä¸&shouldDieãtrueã¨ããæ¡ä»¶ãæºããã®ã§ãEventãä½ãç»é²ããã¦ããã°ãEventDieãå®è¡ãã¾ãã
æå¾ã«shouldDieãfalseã«æ»ãã¾ãã
â¡Player_Deathã¹ã¯ãªãããæ°è¦ä½æ
ç¶ãã¦Player_Deathã¹ã¯ãªãããæ°è¦ä½æãã¦ãPlayerãã¬ããã«åãä»ãã¾ãã以ä¸ã½ã¼ã¹ã§ãã
using UnityEngine; using System.Collections; using UnityEngine.Networking; using UnityEngine.UI; public class Player_Death : NetworkBehaviour { private Player_Health healthScript; //Player_Healthã¹ã¯ãªããã®å¤æ° private Image crossHairImage; //ç §æºã®Image void Start () { //ãã£ãã·ã¥ãã¦ãã crossHairImage = GameObject.Find("Crosshair Image").GetComponent<Image>(); healthScript = GetComponent<Player_Health>(); //Eventãç»é² healthScript.EventDie += DisablePlayer; } //ã¡ã¢ãªã¼ãªã¼ã¯ããæç¨ã®ãå®å ¨ã®ããã®ã¡ã½ãã ãããªã«éè¦ãããªãããã //OnDisable: æ¶æ» ããæã«å¼ã°ãã void OnDisable() { //EventããDisablePlayerã¡ã½ãããåé¤ healthScript.EventDie -= DisablePlayer; } //Eventã§ç»é²ãããã¡ã½ãããCheckConditionã¡ã½ããå ã§ä½¿ããã //åã³ã³ãã¼ãã³ããéã¢ã¯ãã£ãç¶æ ã«ãã void DisablePlayer () { GetComponent<CharacterController> ().enabled = false; GetComponent<Player_Shooting> ().enabled = false; GetComponent<BoxCollider> ().enabled = false; //åãªãã¸ã§ã¯ãã®Rendererãå ¨ã¦æ ¼ç´ Renderer[] renderers = GetComponentsInChildren<Renderer> (); //æ ¼ç´ããRendererå ¨ã¦ãæ¯ã¢ã¯ãã£ãå foreach (Renderer ren in renderers) { ren.enabled = false; } //isDeadãtrueã«ãããã¨ã§ãCheckConditionã®ifæå ã«å ¥ããªãããã«ãã healthScript.isDead = true; if(isLocalPlayer){ GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController> ().enabled = false; crossHairImage.enabled = false; //Respawn Button (次å以éã§Playerã復活ãããããã®ãã¿ã³ãçæããç®æ) } } }
Player_Death.cs
crossHairImageå¤æ°ã¯ã以åä½æããç §æºã®Image(赤ãç¹)ã§ãã
ååãImageã®ã¾ã¾ãªã®ã§ããCrosshair Imageãã¨ããååã«å¤æ´ãã¾ãããã
ç
§æºã®ååããImageãâãCrosshair Imageãã«å¤æ´
Startã¡ã½ããã§crosshairImageã¨healthScriptããã£ãã·ã¥ãã¦ããã¾ãã
ã¾ããå
ã»ã©åºã¦ããEventã®ç»é²ããã®Startã¡ã½ããå
ã§è¡ãã¾ãã
Eventã®ç»é²ã«ã¯ã+=ãã使ãã¾ãã
ç¶ãã¦Eventç»é²ããDisablePlayerã¡ã½ãããè¦ã¦ããã¾ãããã
ã¾ãã¯CharacterControllerãPlayer_ShootingãBoxColliderãæ¯ã¢ã¯ãã£ãåãã¾ãã
BoxColliderã¯ãç¾ç¶ã®ã¾ã¾ã ã¨æµPlayerãæã£ã¦ãä¸ã®ã»ãã¨ä¸ã®ã»ããColliderã®å¤å®ããå¤ãã¦ãã¾ããããªã®ã§ãåç»ã®æå¾ã®ã»ãã§åãä»ãã¦ãã¾ãã
ã§ãã®ã§ãã®ã¿ã¤ãã³ã°ã§åãä»ãã¡ããã¾ãããã
Playerã«Box Collider IsTrigger=true Y=2
æå¹ç¯å²ãåºãã£ã
ç¶ãã¦åãªãã¸ã§ã¯ãã®Rendererãé
åã使ã£ã¦å
¨ã¦åãåºããéã¢ã¯ãã£ãåãã¦ãã¾ãã
å
·ä½çã«ã¯ãPlayer > Capsuleã«ä»ãã¦ããMesh Rendererã¨ãPlayer > FirstPersonCharacter > Cubeã«ã¤ãã¦ããMesh Rendererãéã¢ã¯ãã£ãåãã¦ãã¾ãã
ç¶ãã¦Player_Healthã¹ã¯ãªããã®isDeadå¤æ°ãtrueã«ãã¦ãã¾ããããã«ããif (health <= 0 && !shouldDie && !isDead)ã®æ¡ä»¶ãæºãããã¨ãç¡ããªããCheckConditionã¡ã½ããå ãæå¹ã«ãªããã¨ã¯ç¡ããªãã¾ããã
ç¶ãã¦èªåããã¬ã¤ã¤ã¼èªèº«ã§ããæãFirstPersonControllerã¨crossHairImageãéã¢ã¯ãã£ãããã¦ãã¾ãã
以ä¸ãè²ã ãªãã®ãéã¢ã¯ãã£ãåãã¦ãã¾ããããPlayerã®HPã0以ä¸ã«ãªã£ãæã«å®è¡ããã¾ãã
æå¾ã«OnDisableã¡ã½ããã§ãããããã¯Unityã«åãã£ã¦ããAPIã§ããã®ãªãã¸ã§ã¯ããæ¶æ»
ããæã«å¼ã°ãã¾ãã
ã-=ãã使ã£ã¦DisablePlayerã¡ã½ãããEventãã解é¤ãã¦ãã¾ãã
ã¡ã¢ãªã¼ãªã¼ã¯ããæãªã©ã«ä¸å
·åãçããã®ãé²ãããã«æè¼ãã¦ããããã§ãããããã¾ãèµ·ãããªããããããªã«éè¦ãããªãããã©ä¸å¿ä»ãã¦ããã¿ãããªæãã®ãã¨ãè¨ã£ã¦ã¾ããã
â¢çµæ
ããã§ã¯å®è¡ãã¦è©¦ãã¦ã¿ã¾ãããããã®åã«ãMenuã·ã¼ã³ãéããNetworkManagerã®è¨å®ããã¾ãã
Advanced Configurationã¨Use Network Systemãfalseã«
ãããã¯ã¼ã¯é 延ã®å¿ è¦ã¯ããç¡ããæ¥ç¶ãåãã¦ãå°ããããAdvanced Configurationã¨Use Network Systemãfalseã«ãã¾ãã
ããã§ã¯éçºç»é¢ã¨ãã«ãç»é¢ããããã§å®è¡ãã¦ä¸ããã
æµãæã¤ã¨
æ»ã¬
殺ãããå´ã®ç»é¢
殺ãããã¨å³ä¸ã«è¦ãã¦ããCubeãªãã¸ã§ã¯ãã¨ä¸å¤®ã«ãã£ãç §æºããç»é¢ããæ¶ãã身åããåããªããªãã¾ãã
ãããéçºç»é¢ãè¦ã¦ã¿ãã¨ãPlayerãªãã¸ã§ã¯ãã¯2ä½æ®ã£ãã¾ã¾ã§ãã
殺ããã¦ããªãã¸ã§ã¯ãã¨ãã¦ã¯æ®ã£ã¦ãã
Is Deatãtrueã«ãªããè²ã
ã¨éã¢ã¯ãã£ãåãã¦ãã
ä»ã®ç¶æ ã®ã¾ã¾ããï¼ç»é¢ã²ã¼ã ç»é¢ãå¢ããã¦ã¿ã¾ããããHierarchyãã¥ã¼ã§é©å½ã«å³ã¯ãªãã¯ããReveal Finderãé¸æãFinderãéãã¾ãããã
Finderãéããããã«ããããã¡ã¤ã«ãã³ãããã¦ãå®è¡ãã¦ä¸ãããï¼ãããªã«ç°¡åã«ã²ã¼ã ç»é¢ãå¢ãããã¨ã¯ç¥ããªãã£ãã§ããï¼
ãã«ããã¡ã¤ã«ãã³ããããã ã
ããã§ã¯3ç»é¢ç®ãéãã¾ããããã¨ã
Player3ãç¾ãã
æ»ãã Player2ãé£ã°ãã¦ãPlayer3ãåºç¾ãã¾ããã
ãããªæãã§HPã0以ä¸ã«ãªã£ã¦ããã²ã¼ã ä¸ã«ã¯æ®ãç¶ãã¦ãã¿ã³ãæ¼ãã¦å¾©æ´»ãããããã¦ãããã ã¨æããã¾ãã
ä»åã¯ããã¾ã§ã§ãã
ãããã¨ããããã¾ããã
ã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ã