ãUnity9ãUNETã®latency(é 延æé)ã表示ãã¦ãã¡ãã£ã¨ã ãæ¹åãããUNET4-2ã
Unity5.1.1p3 Personal(2015å¹´7æ)
前回の続きã§ãã
ä»åãã¾ãlatencyã«é¢ãã¦ã§ãé 延æéãç»é¢ã«è¡¨ç¤ºãã¦ããã¾ãã
UNET Part 4 - Latency Simulation! - YouTube
ãç®æ¨ãlatency(é 延æé)ã表示ãã¦ãã¡ãã£ã¨ã ãæ¹åãã
â latency(é 延æé)ãç»é¢ã«è¡¨ç¤ºãã
ã¾ãã¯latencyã表示ããããã®Textãä½æãã¾ããTextã«ã¤ãã¦ã¯ä»¥ä¸ã¨ãã»ã³ã¹ããåç §ä¸ããã
ã¾ãã¯Mainã·ã¼ã³ãéããHierarchyãã¥ã¼ã®Create > UI > Textãé¸æããååãLatency Textã«å¤æ´ãã¾ãã
Latency Textä½æ
Anchor Presetsã¯å·¦ä¸ã«ããBest Fitãtrueã«ãã¦ä¸ããã
ç¶ãã¦latencyãåå¾ããããã®ã¹ã¯ãªããä¿®æ£ã§ãã
latencyã¯NetworkClientã®GetRTTã¡ã½ããã§åå¾ããã§ãã詳細ã¯公式サイトのマニュアルã«æ¸ãã¦ããã¾ãã
ã¨ããããã§ã以ä¸ã½ã¼ã¹ã§ãã
using UnityEngine; using System.Collections; using UnityEngine.Networking; //********** éå§ **********// //GUI表示ç¨ã©ã¤ãã©ãª using UnityEngine.UI; //********** çµäº **********// public class Player_SyncPosition : NetworkBehaviour { [SyncVar] private Vector3 syncPos; [SerializeField] Transform myTransform; [SerializeField] float lerpRate = 15; private Vector3 lastPos; private float threshold = 0.5f; //********** éå§ **********// private NetworkClient nClient; private int latency; //é 延æé private Text latencyText; //é 延æé表示ç¨ããã¹ã void Start () { //NetworkClientã¨Textããã£ãã·ã¥ãã nClient = GameObject.Find("NetworkManager").GetComponent<NetworkManager>().client; latencyText = GameObject.Find("Latency Text").GetComponent<Text>(); } //********** çµäº **********// void Update () { LerpPosition(); //********** éå§ **********// ShowLatency(); //********** çµäº **********// } void FixedUpdate () { TransmitPosition(); } void LerpPosition () { if (!isLocalPlayer) { myTransform.position = Vector3.Lerp(myTransform.position, syncPos, Time.deltaTime * lerpRate); // Debug.Log(Time.deltaTime.ToString()); } } [Command] void CmdProvidePositionToServer (Vector3 pos) { syncPos = pos; // Debug.Log("Command"); } [ClientCallback] void TransmitPosition () { if (isLocalPlayer && Vector3.Distance(myTransform.position, lastPos) > threshold) { CmdProvidePositionToServer(myTransform.position); lastPos = myTransform.position; } } //********** éå§ **********// void ShowLatency () { if (isLocalPlayer) { //latencyãåå¾ latency = nClient.GetRTT(); //latencyã表示 latencyText.text = latency.ToString(); } } //********** çµäº **********// }
Player_SyncPosition.cs
NetworkClientã®GetRTTã使ã£ã¦é
延æé(1/1000ç§ã§åå¾)ãåå¾ãã表示ãã¦ãã¾ãã
é
延æéãç»é¢ã«è¡¨ç¤ºããã ããªã®ã§ãShowLatencyã¡ã½ããã¯Updateã§ãFixedUpdateã§ãã©ã¡ãããå¼ã³åºãã¦ãOKã§ãã
ããã§ã¯ãã«ãå´ããã¹ãã«ãéçºç»é¢å´ãã¯ã©ã¤ã¢ã³ãã«ãã¦ãã²ã¼ã ãå®è¡ãã¦ä¸ããã
409ï¼0.409ç§ã®é
延
ã¯ã©ã¤ã¢ã³ããããã¹ãã¸ãã¼ã¿ãéãããã¹ããããã¼ã¿ãè¿ã£ã¦ããã¾ã§ã«ã0.4ç§ããã£ã¦ãã¾ã£ã¦ããããã§ããããã¯ããªãé
ãããã§ããchoppyï¼ã¨ããã¨ããï¼ãªæãã«ãªã£ã¦ãã¾ãã
â¡sendIntervalãå¤æ´ãã¦åæãã¹ã ã¼ãºã«ãã
é 延æéãç»é¢ã«è¡¨ç¤ºãããããã«ãªã£ãã®ã§ã次ã¯é 延ããªãã¹ããªããã¦ããã¾ãããã
cannnelã使ããããå ã»ã©å°ãã ã触ããAdvanced Configurationãæå¹ã«ãã¾ãã
Timeoutsã®é¨åã«ã¤ãã¦ãåç»ã§ã¯å°ã触ãã¦ãã¾ããæããããã§ã¯ãåç·é度ãè½ã¡ãæã«æ¥ç¶ãåãåºæºãè¨å®ã§ãã¾ãã
ç¶ãã¦Player_SyncPositionã¹ã¯ãªããã«NetworkSettingsãå ãã¦ãchannelã¨sendIntervalãè¨å®ãã¦ããã¾ãã
using UnityEngine; using System.Collections; using UnityEngine.Networking; using UnityEngine.UI; //********** éå§ **********// [NetworkSettings(channel=0, sendInterval=0.033f)] //********** çµäº **********// public class Player_SyncPosition : NetworkBehaviour { ãçç¥ã }
Player_SyncPosition.cs
NetworkSettingsãã¯ã©ã¹åã®ä¸ã«ä¸è¡è¿½å ããã ãã§ãã
sendIntervalã¯ãã¤ã³ã¿ã¼ãã«ã¨ããã ããã£ã¦ãã¼ã¿ã®éä¿¡ã¨éä¿¡ã®éã®æéã ã¨æãã®ã§ãããããåããã¾ããã
NetworkSettingsæ å ±ã¯ãInspectorãã¥ã¼ããã確èªã§ãã¾ãã
NetworkSettingsæ
å ±
ããã§ã¯ã²ã¼ã ãåããã¦ã¿ã¦ç¢ºèªãã¦ã¿ã¾ãããã
ãªãã¨ãªã軽æ¸ãããããªã»ã»ã»
è¥å¹²ãããªã£ãæãã¯ããã¾ãããæ£ç´ãã¾ãå¤åã¯ããã¾ããã§ããã
UNETåç»é²ãã¦ããä¸ã§å°ããã¤æ¹åããã¦ããã®ã§ããããã
â¢å¾ãã®ã¹ã¯ãªããã«ãé©ç¨ãã
次ã«Player_SyncRotationã¹ã¯ãªããã«ãåãå¦çã追å ãã¦ããã¾ãã以ä¸ã½ã¼ã¹ã§ãã
using UnityEngine; using UnityEngine.Networking; using System.Collections; public class Player_SyncRotation : NetworkBehaviour { [SyncVar] private Quaternion syncPlayerRotation; [SyncVar] private Quaternion syncCamRotation; [SerializeField] private Transform playerTransform; [SerializeField] private Transform camTransform; [SerializeField] private float lerpRate = 15; private Quaternion lastPlayerRot; private Quaternion lastCamRot; private float threshold = 5; void Start () { } //********** éå§ **********// void Update () { LerpRotations(); } //********** çµäº **********// void FixedUpdate () { TransmitRotations(); } ãçç¥ã }
Player_SyncRotation.cs
Time.deltaTimeã使ããã¦ãLerpRotationsã¡ã½ããããUpdateã¡ã½ããããå¼ã³åºãããã«ãã¾ããã
â£ãã¬ã¤ã¤ã¼å士ã®è¡çªå¤å®ãæå¹ã«ãã
ãã¬ã¤ã¤ã¼å士ã®è¡çªãç¡å¹ã«ãªã£ã¦ããã®ã§ããããæå¹ã«ãã¾ãããã
ã¾ãã¯Playerãã¬ããã®Character Controllerãæå¹ã«ãã¾ãã
ãã§ãã¯ãã¦æå¹ã«ãã
次ã«Player_NetworkSetupã®ä¿®æ£ããã¾ãã以ä¸ã½ã¼ã¹ã§ãã
using UnityEngine; using System.Collections; using UnityEngine.Networking; public class Player_NetworkSetup : NetworkBehaviour { [SerializeField]Camera FPSCharacterCam; [SerializeField]AudioListener audioListener; void Start () { if (isLocalPlayer) { GameObject.Find("Scene Camera").SetActive(false); //********** ã³ã¡ã³ãã¢ã¦ã **********// // GetComponent<CharacterController>().enabled = true; //********** ã³ã¡ã³ãã¢ã¦ã **********// GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController>().enabled = true; FPSCharacterCam.enabled = true; audioListener.enabled = true; } } }
CharacterControllerãæå¹ã«ãã¦ããå¦çãã³ã¡ã³ãã¢ã¦ããã¾ããã
以ä¸ã§ãã¬ã¤ã¤ã¼éã®è¡çªãæå¹ã«ãªãã¾ããã
æå¹ã ããä¸ã«ä¹ãã
ä»åã¯ããã¾ã§ã§ãã
ãããã¨ããããã¾ããã
ã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ã