ãUnity9ãUNETã§HPãç»é¢ã«è¡¨ç¤ºããPlayerã¸ãã¡ã¼ã¸ãä¸ãããUNET9ã
Unity5.1.1p3 Personal(2015å¹´7æ)
前回の続きã§ãã
ååã¾ã§ã§æµPlayerã®ååãåå¾ããã¨ããã¾ã§ããã¾ããã
ä»åã¯Playerã«HPãä»ãã¦ãæµPlayerã¸ãã¡ã¼ã¸ãä¸ãããèªåããã¡ã¼ã¸ãåããããã«ãã¾ãã
UNET Part 9 - Taking Damage! - YouTube
ãç®æ¨ãHPãç»é¢ã«è¡¨ç¤ºããPlayerã¸ãã¡ã¼ã¸ãä¸ãã
â HPãç»é¢ã«è¡¨ç¤ºãã
ã¾ãã¯HPãç»é¢ã«è¡¨ç¤ºãã¾ãããã
æ°ããPlayer_Healthã¹ã¯ãªãããä½æããPlayerãã¬ããã«åãä»ãã¦ä¸ããã
using UnityEngine; using System.Collections; using UnityEngine.Networking; using UnityEngine.UI; public class Player_Health : NetworkBehaviour { //SyncVar: Clientã®å¤æ°å¤æ´æ å ±ããµã¼ãã¼ãåãåããå¤æ´çµæãå ¨ã¯ã©ã¤ã¢ã³ãã¸éã [SyncVar] private int health = 100; private Text healthText; // Use this for initialization void Start () { //Textãªãã¸ã§ã¯ãããã£ãã·ã¥ healthText = GameObject.Find("Health Text").GetComponent<Text>(); SetHealthText(); } // Update is called once per frame void Update () { } void SetHealthText () { if (isLocalPlayer) { //ç¾å¨HPãç»é¢ã«è¡¨ç¤º healthText.text = "Health " + health.ToString(); } } //ãã¡ã¼ã¸ãåããæã®ã¡ã½ãã public void DeductHealth(int dmg) { health -= dmg; } }
Player_Health.cs
Startã¡ã½ããã§Textãªãã¸ã§ã¯ãããã£ãã·ã¥ããSetHealthTextã¡ã½ãããå¼ã³åºããç»é¢ã«HPã表示ãã¦ãã¾ãã
ã¾ããDeductHealthã¡ã½ãããå¤é¨ããå¼ã³åºããhealthãå¼æ°åã ãæ¸ããã¦ãã¾ãã
ããã§ã²ã¨ã¾ããèªåã®HPãç»é¢ã«è¡¨ç¤ºããããã¨ãã§ãã¾ããã
ç»é¢ã«HPã表示
â¡Playerã«ãã¡ã¼ã¸ãä¸ãã
ç¶ãã¦Playerã«ãã¡ã¼ã¸ãä¸ãã¦ããã¾ãã
ã¾ãã¯ååä½æããPlayer_Shootingã¡ã½ãããä¿®æ£ãã¾ãã
using UnityEngine; using System.Collections; using UnityEngine.Networking; public class Player_Shooting : NetworkBehaviour { //********** éå§ **********// private int damage = 25; //floatåâintå //********** çµäº **********// private float range = 200; [SerializeField] private Transform camTransform; private RaycastHit hit; void Update () { CheckIfShooting(); } void CheckIfShooting () { if (!isLocalPlayer) { return; } if (Input.GetKeyDown(KeyCode.Mouse0)) { Shooting(); } } void Shooting () { if (Physics.Raycast (camTransform.TransformPoint (0, 0, 0.5f), camTransform.forward, out hit, range)) { if (hit.transform.tag == "Player") { string uIdentity = hit.transform.name; CmdTellServerWhoWasShot(uIdentity, damage); } } } [Command] //dmgå¤æ°ãfloatåâintå //********** éå§ **********// void CmdTellServerWhoWasShot (string uniqueID, int dmg) { //********** çµäº **********// GameObject go = GameObject.Find(uniqueID); //********** éå§ **********// //Player_Healthã®DeductHealthã¡ã½ããå¼ã³åºã go.GetComponent<Player_Health>().DeductHealth(dmg); //********** çµäº **********// } }
Player_Shooting.cs
CmdTellServerWhoWasShotã¡ã½ããå ã§ãå ã»ã©ä½æããPlayer_Healthã¹ã¯ãªããã®DeductHealthã¡ã½ãããå¼ã³ã ãã¦ãã¾ãã
ç¶ãã¦Player_Healthãä¿®æ£ãã¦ããã¾ãã以ä¸ã½ã¼ã¹ã§ãã
using UnityEngine; using System.Collections; using UnityEngine.Networking; using UnityEngine.UI; public class Player_Health : NetworkBehaviour { //********** éå§ **********// //[Command]ã¡ã½ããå ã§healthãå¤æ´ãããæã«æå®ã¡ã½ãããå¼ã³åºã [SyncVar (hook = "OnHealthChanged")] private int health = 100; //********** çµäº **********// private Text healthText; // Use this for initialization void Start () { healthText = GameObject.Find("Health Text").GetComponent<Text>(); SetHealthText(); } // Update is called once per frame void Update () { } void SetHealthText () { if (isLocalPlayer) { healthText.text = "Health " + health.ToString(); } } //[Command]ã®ä»ãã¦ããCmdTellServerWhoWasShotã¡ã½ããããå¼ã°ããã®ã§ã //hookãä½åããï¼ public void DeductHealth(int dmg) { //hookãä½åãããã£ããã¨ãªãã ãã§ããã®æç¹ã§ã¯ã¾ã å ¨ã¯ã©ã¤ã¢ã³ãã¸ã¯éä¿¡ããã¦ããªã health -= dmg; } //********** éå§ **********// void OnHealthChanged (int hlth) { //å ¨ã¯ã©ã¤ã¢ã³ãã¸å¤æ´çµæãéä¿¡ health = hlth; //HPã®è¡¨ç¤ºãå¤æ´ SetHealthText(); } //********** çµäº **********// }
Player_Health.cs
Player_Shootingã¹ã¯ãªããã®CmdTellServerWhoWasShotã¡ã½ãããã DeductHealthã¡ã½ãããå¼ã³åºããã¦ãSyncVarå¤æ°ã®healthãå¤æ´ãããhookãä½åãã¦OnHealthChangedã¡ã½ãããå®è¡ããããã¨ããæµãã§ãã
ä»ã¾ã§ã¯SyncVarãæ©è½ããã®ã¯SyncVarå¤æ°ã[Command]ãã¤ãã¦ããã¡ã½ããå ã§å¤æ´ããæã®ã¿ã ã£ãã®ã§å°ãæ¸æãã¾ããããæããDeductHealthã¡ã½ããã®å¼ã³åºãå ã§ããCmdTellServerWhoWasShotã¡ã½ããã«[Command]ãã¤ãã¦ãããã大ä¸å¤«ã¨ãããã¨ã ã¨æãã¾ãã
ã¨ã«ããhealthã®å¤æ´çµæãå
¨ã¯ã©ã¤ã¢ã³ãã¸ç¥ãããç»é¢ä¸ã®èªåã®HPãå¤æ´ãããã¨ãã§ãã¾ããã
â¢çµæ
Playerã¸åãã¦æ»æããçµæãç¡äºHPãæ¸ãããã¨ãã§ãã¾ããã
ãã¡ã¼ã¸ï¼
HPã0ã«ãªã£ããä»ã®ã¨ãããã¤ãã¹ã«ãªã£ã¡ãã
次å以éã§HPããã¤ãã¹ã«ãªã£ãæããã£ã©ã¯ã¿ã¼ãæ»ãã ãã¨ãã£ãå¦çã追å ããã¦ãããã ã¨æãã¾ãã
ããã§ã¯ä»åã¯ããã¾ã§ã§ãã
ãããã¨ããããã¾ããã
ã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ã