ãUnity9ãUNETã§ãã«ããã¬ã¤ã¤ã¼ãªãªã³ã©ã¤ã³ã²ã¼ã éçºãUNET1ã
Unity5.1.1f1 Personal(2015å¹´7æ)
ãã«ããã¬ã¤ã¤ã¼ãããã¯ã¼ãã³ã°ã·ã¹ãã ã§ããUNETããUnity5.1ããæ¨æºæè¼ããã¾ããï¼
ããã¦ãå°ãåããUNETã®ãã¥ã¼ããªã¢ã«ãããã£ã¦ããã®ã§ãä»åããè¨äºã«ã¾ã¨ãã¦ããããã¨æãã¾ãã
ä»å¾ããã°è¨äºãã¢ãããã¦ããä¸ã§ããåãããªããã©åããã¨ãããã¿ã¼ã³ãã誤ã£ã説æçæãç¹ãå¤ã ãããã¨æãã¾ãã
ééããªã©ãæ°ä»ãã®ç¹ãããã¾ããããã³ã¡ã³ãçã§æãã¦ä¸ããã¨å©ããã¾ãã
UNET Part 1 - Setup and Movement Syncing - YouTube
ãç®æ¨ããã¹ããã¬ã¤ã¤ã¼ã¨ã¯ã©ã¤ã¢ã³ããã¬ã¤ã¤ã¼ã«å¥ã ã®åããããã
â ä¸æºå
ã¾ãã¯ãMainãã¨ãMenuãã¨ããååã§ãSceneã2ã¤ä½ãã¾ãã
ä»ããç·¨éããã®ã¯Mainã·ã¼ã³ã§ãã®ã§ãMainã·ã¼ã³ãéãã¦ããã¦ä¸ããã
次ã«Planeãä½æããSizeãX:100,Z:100ã«å¤æ´ããç°è²ã®Materialãä½æãåãä»ãã¦ä¸ããã
ç¶ãã¦Cubeãä½æããPositionãY:0.5ã«å¤æ´ãã¦ä¸ããã
ã¾ãä½æããCubeãã³ãã¼ãã¦ããã²ã¨ã¤Cubeãä½ããé©å½ãªå ´æã¸é
ç½®ãã¦ä¸ããã
ã³ãã¼ããCubeã®Scaleã¯X,Y,Zå
¨ã¦2ã«å¤æ´ãã¦ããã¾ãããã
Mainã·ã¼ã³ã«åº1ã¤ã¨Cube2ã¤ãé
ç½®
æå¾ã«Environmentã¨ããååã§ç©ºã®ã²ã¼ã ãªãã¸ã§ã¯ããä½æããPlaneã¨Cube2ã¤ãåãªãã¸ã§ã¯ããã¦ä¸ããã
ãã£ã¼ã«ãã¯Environmentãªãã¸ã§ã¯ãã®åãªãã¸ã§ã¯ãã¨ãã
â¡Playerä½æ
ç¶ãã¦Playerãä½æãã¾ããAssetsããImport Package>Charactersãé¸æããStandard Assetãç¨æãã¾ãã
Characteré¸æ
ç¶ãã¦Standard Asset>Characters>FirstPersonCharacter>Prefabã«FPSControllerã¨ãããã¬ãããããã®ã§ãHierarchyä¸ã¸ãã©ãã°ï¼ãããããã¦ä¸ããã
ååã¯ãPlayerãã«å¤æ´ãã¾ãã
ãã©ãã°ï¼ããããï¼
次ã«Prefabsãã©ã«ããä½æããPlayerãªãã¸ã§ã¯ãããã©ãã°ï¼ãããããã¦ãã¬ããåãã¾ãããã
ãã©ãã°ï¼ããããï¼
Playerã®åãªãã¸ã§ã¯ãã¨ãã¦Capsuleãªãã¸ã§ã¯ããä½æãã¾ãã
Capsuleãªãã¸ã§ã¯ãã«æåããä»ãã¦ããCapsule Colliderã¯Remove Componentã§æ¶ãã¦ä¸ããã
Capsuleãªãã¸ã§ã¯ããåãªãã¸ã§ã¯ãã«ãã
ä»ã®ã¾ã¾ã ã¨å°é¢ã«åã¾ã£ã¦ãã¾ãã®ã§ãPlayerã®PositionãX:0, Y: 1, Z: 0ã«å¤æ´ãã¾ãã
ã¨ããããä¸äººç§°è¦ç¹ã§ãã£ã¼ã«ããåãPlayerãå®æãã¾ãããã²ã¼ã ãå®è¡ããã¨ãç¢å°ãã¼ã§ç§»åãã¹ãã¼ã¹ãã¼ã§ã¸ã£ã³ãããã¦ã¹ãåãããã¨ã§è¦ç¹ãå¤ããã¾ãã
ããã¾ã§ã¯ãããã¯ã¼ã¯ã¨å ¨ãé¢ä¿ãªãé¨åã§ãã®ã§ãæ··ä¹±ããã«ãããããã¨æãã¾ãã
â¢NetworkManagerãä½æãã
ãããããããããããã¯ã¼ã¯ã¨é¢ä¿ãã¦ãã¾ãï¼
ã¾ãã¯ãNetworkManagerãã¨ããååã§ç©ºã®ã²ã¼ã ãªãã¸ã§ã¯ããä½æãã¦ä¸ããã
次ã«Inspectorãã¥ã¼ããAddComponent>Network>NetworkManagerãåãä»ãã¦ä¸ããã
NetworkManagerã³ã³ãã¼ãã³ã
åç»ã§ã¯OffLineã®æã«Menuã·ã¼ã³ããOnlineã®æã«Mainã·ã¼ã³ãæå®ãããã£ãããã§ãããã¾ããã£ã¦ããªããããªã®ã§ãOffline Sceneã¨Online Sceneã®è¨å®ã¯ä»åã¯ã¾ã ãã¾ããã
ä»åã¯ã¤ã³ã¿ã¼ãããè¶ãã§ã¯ãªãèªåã®ãã½ã³ã³å (ãã¼ã«ã«ç°å¢)ã§UNETã使ãã®ã§ãNetwork Infoã®NetworkAddressã¯localhost(127.0.0.1ã¨å ¥åãã¦ãåãæå³)ã®ã¾ã¾ã«ãã¦ããã¦ä¸ããã
ãã¼ãã¯å¤é¨ãããã¼ã¿ãåãåãæã®å ¥ãå£ã§ãä»åã¯ãã¼ã«ã«ç°å¢ã§å®çµãããã®ã§ãNetwork Portã«ã¤ãã¦ãä»åã¯è¨å®ããå¿ è¦ã¯ããã¾ããã
Spawn Infoã§ã¯ããããã¯ã¼ã¯ã«æ¥ç¶ããæã«ä½æãããèªåãæä½ãããã¬ã¤ã¤ã¼ãè¨å®ãããã¨ãã§ãã¾ããæ©éPlayer Prefabã«ãå ã»ã©ä½æããPlayerãã¬ãã¡ããè¨å®ãã¾ãããã
ãã©ãã°ï¼ããããããã¨ã»ã»ã»
NetworkIdentityãç¡ãã§ããã¨ããã¨ã©ã¼ãçºçãã
ã¨ããããã§ãPlayerãã¬ããã«NetworkIdentityã³ã³ãã¼ãã³ããä»ãã¦åé¿ãã¾ãããã
Hierarchyä¸ã«ããPlayerãã¬ããã«Add Component>Network>NetworkIdentityã§åãä»ããLocal Player Authorityã«ãã§ãã¯ãå
¥ãã¦ä¸ããã
Local Player Authorityã«ãã§ãã¯ãå
¥ãããã¨ã§ãä»ã®ãã¬ã¤ã¤ã¼ã¨èªåãæä½ãããã¬ã¤ã¤ã¼ãåºå¥ãããããã«ãªãã¾ãã
ããã¾ã§ã§ãããApplyãã¿ã³ãæ¼ãã¦ä¸ãããProjectãã¥ã¼ä¸ã®Playerãã¬ããã«å¤æ´ãåæ ããã¾ãã
Network Identityãåãä»ãApplyãã¿ã³ãæ¼ããã¨ã§å¤æ´ãåæ
NetworkManagerã®Spawn Info>Player Prefabã«ãPlayerãã¬ãããè¨å®ã§ããããã«ãªã£ã
ç¶ãã¦NetworkManagerHUD(ãããã¢ãããã£ã¹ãã¬ã¤)ã³ã³ãã¼ãã³ããåãä»ãã¾ããGUIã§ãããã¯ã¼ã¯ã«æ¥ç¶ããããæ示ã§ããããã«ãªãã¾ãã
NetworkManagerã®Inspectorãã¥ã¼ããAdd Component>Network>NetworkManagerHUDãé¸æãã¦ä¸ãããåãä»ããã ãã§ãç¹ã«è¨å®ãããã¨ãªã使ãã¾ãã
ã²ã¼ã ãå®è¡ããã¨GUIãåºã¦ãã
â£Playerã®åããã¤ã³ã¹ã¿ã³ã¹ãã¨ã«åãã
ä»ã®ç¶æ
ã§æ¢ã«èªãã£ã©ã¨ç¸æãã£ã©ã®2ãªãã¸ã§ã¯ããä½æãã¦åãããã¨ã¯ã§ãã¾ãããçæ¹ã®ãªãã¸ã§ã¯ããåãããæãããçæ¹ã®ãªãã¸ã§ã¯ããå
¨ãåãåãããã¦ãã¾ãç¶æ
ã«ããã¾ãã
対çã¨ãã¦ãã¾ãã¯Hierarchyã«ããPlayerãªãã¸ã§ã¯ãã«Network Transformã³ã³ãã¼ãã³ããä»ãã¾ãããããä»ãããã¨ã§ãä½ç½®æ å ±ãåæããããã¨ãã§ããããã§ãã
ãã¦ãã¾ãã¯NetworkTransformã®Tramsform Sync ModeãSync Rigidbody 3DããSync Transformã«å¤æ´ãã¾ããããCharacter Controllerã¨First PersonController(Script)ã使ã£ã¦ç§»åãã¦ããå ´åã¯ãSync Transformã«å¤æ´ããããã§ãã
ç¶ãã¦Playerãæ¹åãå¤æ´ããæã«å¤æ´ãããã¨ãåãããããããã«ãéç ²ã®ãããªãã®ãåãä»ãã¾ããPlayerã®åãªãã¸ã§ã¯ãã«Cubeãåãä»ããTransformã以ä¸ã®ãããªè¨å®ã«ãã¦ä¸ãããã¾ããBox Colliderã¯åãé¤ãã¦ããã¦ä¸ããã
次ã«Playerãªãã¸ã§ã¯ããApplyãã¦å¤æ´ãåæ ããHierarchyä¸ã«ããPlayerãªãã¸ã§ã¯ãã¯åé¤ãã¾ãã
ããã¾ã§æºåãã§ããããBuildãã¦å®éã«åããã¦ã¿ã¾ããããFile>Build Settingsãé¸æããAdd Currentã§Mainã·ã¼ã³ããã«ãã®å¯¾è±¡ã«ããBuild And Runãå®è¡ãã¾ãã
Buildãã¦å®è¡
ããã§2人ã®ãã¬ã¤ã¤ã¼ã1ã¤ã®ã²ã¼ã 空éã§åæã«åå¨ã§ããããã«ãªãã¾ããã
å·¦å´ããã«ããã¦ä½ã£ãã²ã¼ã ç»é¢ãå³å´ãéçºç°å¢ã®ã²ã¼ã ç»é¢ã§ãã
ããã§ã¯éçºç°å¢ã®ã»ãããã¹ãããã«ãç°å¢ã®ã»ããã¯ã©ã¤ã¢ã³ãã§éãã§ã¿ã¾ããããã¾ãã¯éçºç°å¢ã§ãLAN Host(H)ãã¨æ¸ããããã¿ã³ãã¯ãªãã¯ãã¦é©å½ã«åããã¦ä¸ããã
次ã«ãã«ãç°å¢ã®ã»ãã§ãLAN Client(C)ãã¨æ¸ããããã¿ã³ãã¯ãªãã¯ãã¦ä¸ããã
ããã¨ãç¸æãã¬ã¤ã¤ã¼ã確èªãããã¨ãã§ãã¾ãã
ç¸æãã¬ã¤ã¤ã¼ããã
ãããä»ã®ç¶æ ã ã¨å ã»ã©èª¬æããããã«ã2ãªãã¸ã§ã¯ããå ¨ãåãåãããã¦ãã¾ãã¾ãã
ãã®ç¶æ ãåé¿ããããã«ãPlayerãã¬ããã«ä»ãã¦ããã³ã³ãã¼ãã³ããéã¢ã¯ãã£ãç¶æ ã«ãã¦ãããPlayerãã¬ãããçæããæã«åã³ã³ãã¼ãã³ããã¢ã¯ãã£ãåããå¿ è¦ãããã¾ãã
ã¾ãã¯Playerã«ä»ãã¦ããCharacter Controllerã¨First Person Controllerãéã¢ã¯ãã£ãåãã¾ãã
ãã§ãã¯ãå¤ãã¦éã¢ã¯ãã£ãå
次ã«Playerã®åãªãã¸ã§ã¯ãã§ããFirstPersonCharacterã®Cameraã¨Audio Listenerãéã¢ã¯ãã£ãåãã¾ãã
ãã§ãã¯ãå¤ãã¦éã¢ã¯ãã£ãå
æå¾ã«Main Cameraã®ååããScene Cameraãã«å¤æ´ãã¦ä¸ããã
ããã§ã¯ãã³ã³ãã¼ãã³ããæ¯ã¢ã¯ãã£ãããã¢ã¯ãã£ãã¸å¤æ´ãããPlayer_NetworkSetupãã¹ã¯ãªãããä½æãã¦ããã¾ãã以ä¸ã½ã¼ã¹ã§ãã
using UnityEngine; using System.Collections; using UnityEngine.Networking; //ãããã¯ã¼ã¯é¢é£ã§å¿ è¦ãªã©ã¤ãã©ãªã¼ //ç¶æ¿ã¯MonoBegaviourã§ã¯ãªãNetworkBehaviour public class Player_NetworkSetup : NetworkBehaviour { //SerializeField: publicã§ãªãã¦ãInspectorä¸ã§å¤æ°å 容ãæå®ã§ãã [SerializeField]Camera FPSCharacterCam; //FirstPersonCharacterã®Camera [SerializeField]AudioListener audioListener; //FirstPersonCharacterã®Audio Listener void Start () { //èªåãæä½ãããªãã¸ã§ã¯ãã«éå®ãã if (isLocalPlayer) { //FPSCharacterCamã使ããããScene Cameraãéã¢ã¯ãã£ãå GameObject.Find("Scene Camera").SetActive(false); GetComponent<CharacterController>().enabled = true; //Character Controllerãã¢ã¯ãã£ãå //FirstPersonControllerãã¢ã¯ãã£ãå GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController>().enabled = true; //FirstPersonCharacterã®åã³ã³ãã¼ãã³ããã¢ã¯ãã£ãå FPSCharacterCam.enabled = true; audioListener.enabled = true; } } }
Player_NetworkSetup.cs
ã¾ãã¯Serialize Fieldã«ãCameraã¨Audio Listenerãè¨å®ãã¦ããã¾ããããPlayerã®åãªãã¸ã§ã¯ãã§ããFIrstPersonCharacterããããããã©ãã°ï¼ãããããã¦ãããã°æå®ã§ãã¾ãã
ãã©ãã°ï¼ããããï¼
ããã§ã¯ã¹ã¯ãªããã«å ¥ãã¾ãã
isLocalPlayerãtrueã®æã®ã¿Startã¡ã½ãããå®è¡ããããã«ãªã£ã¦ãã¾ãã
isLocalPlayerã¯èªåãæä½ããPlayerã«å¯¾ãã¦ã®ã¿trueã«ãªãã¾ãã®ã§ãç»é¢ä¸ã«Playerã2人ãã¦ãèªåãæä½ããPlayerã®ã³ã³ãã¼ãã³ãã®ã¿ãã¢ã¯ãã£ãåãããã¨ãã§ãã¾ãã
isLocalPlayerå¤æ°ã使ãããã«ã¯NetworkBehaviourãç¶æ¿ããå¿ è¦ãããã¾ããããã¦ãNetworkBehaviourãç¶æ¿ããããã«ã¯ãusing UnityEngine.Networkingãã¨ã¹ã¯ãªããã®é ã«æ¸ãã¦ããå¿ è¦ãããã¾ãï¼
å度Buildãè¡ãåä½ã確èªãã¦ã¿ãã¨ãä»åº¦ã¯èªãã¬ã¤ã¤ã¼ã¨ç¸æãã¬ã¤ã¤ã¼ãå¥ã ã®åãããã¾ãï¼
åç»ãpart1ã®éä¸ã§ããé·ããªã£ã¦ãã¾ã£ãã®ã§ãä»åã¯ããã¾ã§ã§ãã
ãããã¨ããããã¾ããã
ã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ã