ãUnity9ãUNETã§ã¾ã³ãAIãåºç¾ãããï¼ãUNET12ã
Unity5.1.1p3 Personal(2015å¹´7æ)
前回の続きã§ãã
ä»åã¯ã¾ã³ãAIãä½æãã¦ããã¾ãã
UNET Part 12 - Zombies! - YouTube
ãç®æ¨ãã¾ã³ãAIãåºç¾ããã
â ã¾ã³ããªãã¸ã§ã¯ããä½æãã
ã¾ãã¯ã¾ã³ãAIãä½ã£ã¦ããã¾ããããHierarchyãã¥ã¼ããCreate > 3DObject > Cubeãé¸æããååããZombieãã«ããTransformã以ä¸ã®ããã«è¨å®ãã¾ãã
Zombieãªãã¸ã§ã¯ãä½æ
ç¶ãã¦Materialãä½æããZombieãªãã¸ã§ã¯ãã«åãä»ãã¾ãã
ååã¯ãZombie Greenãã§ãè²ã¯ç·è²ã§ãã
Materialä½æ
ç¶ãã¦Zonbieãªãã¸ã§ã¯ãã«Nav Mesh Agentã³ã³ãã¼ãã³ããåãä»ãã¦ããã¾ããAdd ComponentããNavigation > Nav Mesh Agentãé¸æãã¦ä¸ããã
Nav Mesh Agentã¯ãæå®ããç¯å²å
ã§AIã移åãããããã®æ©è½ã§ãã
移åç¯å²ãéå®ããã¦ãããããé害ç©ãèªåçã«é¿ããããããããæã«ä¾¿å©ã§ãã
Agent Sizeã®Radius(åå¾)ã0.6ã«ããAIã®åå¾ãè¥å¹²å¤§ãããã¾ãã
ã¾ããStopping Distanceã2.5ã«ããã¿ã¼ã²ãã(Player)ã®æåã§æ¢ã¾ãããã«ãã¾ãã
Nav Mesh Agentè¨å®
ç¶ãã¦Zombieãªãã¸ã§ã¯ãããã¬ããåãã¾ãã
ãã©ãã°ï¼ããããï¼ãå
ãªãã¸ã§ã¯ãã¯åé¤
æå¾ã«AIã®ç§»åç¯å²ãè¨å®ãã¾ããWindow > Navigationãé¸æãã
Window > Navigation
Navigationãã¥ã¼ã«ã¦Mesh Rendererãé¸æãã¾ããããã¨ãHierarchyãã¥ã¼ã«Mesh Rendererã®ã¤ãããªãã¸ã§ã¯ãã®ã¿ã表示ããã¾ãã
表示ããããªãã¸ã§ã¯ããå
¨ã¦é¸æããNavigation Staticã«ãã§ãã¯ãå
¥ãã¦ä¸ããã
Navigation Staticã«ãã§ãã¯
次ã«Bakeãã¥ã¼ã«åãæ¿ããããã©ã«ãè¨å®ã®ã¾ã¾Bakeãã¿ã³ãæ¼ãã¾ããããã¨ãNavigationãã¥ã¼ä¸ã«Exporting Tilesã¨ãããã¼ãåºã¦ãã¾ãã
Mesh Rendererã«AIãåããç¯å²ãç¼ãã¦ããã¨ãããªã®ã§ããã¼ãæºã¿ã³ã«ãªãã¾ã§å¾
ã¡ã¾ãããã
Bakeãã¿ã³ãã¯ãªãã¯
â¡AIç¨ã®ã¹ã¯ãªãããä½æãã
ã¾ã³ãAIã®å
ã¨ãªããªãã¸ã§ã¯ãã®ä½æã¨ãAIã移åã§ããç¯å²æå®ãå®äºãã¾ããã
ç¶ãã¦AIç¨ã®ã¹ã¯ãªãããä½æãã¦ããã¾ãã
ååã¯Zombie_Targetã«ãã¾ãã以ä¸ã½ã¼ã¹ã§ãã
using UnityEngine; using System.Collections; using UnityEngine.Networking; public class Zombie_Target : NetworkBehaviour { private NavMeshAgent agent; private Transform myTransform; private Transform targetTransform; //ã¾ã³ããæ¢ç¥ããã¬ã¤ã¤ã¼ private LayerMask raycastLayer; //ã¾ã³ããPlayerãæ¢ç¥ããåå¾ private float radius = 100f; // Use this for initialization void Start () { agent = GetComponent<NavMeshAgent>(); myTransform =transform; raycastLayer = 1<<LayerMask.NameToLayer("Player"); } // Update is called once per frame void FixedUpdate () { SearchForTarget(); MoveForTarget(); } void SearchForTarget () { //ãµã¼ãã¼ãããªããã°ã¡ã½ããçµäº if (!isServer) { return; } //Playerãã¾ã åå¾ãã¦ããªãæ if (targetTransform == null) { //Physics.OverlapSphere: ããå°ç¹ãä¸å¿ã«çãä½ããè¡çªãããªãã¸ã§ã¯ããåå¾ãã //第1å¼æ°: ä¸å¿ç¹ 第2å¼æ°: åå¾ ç¬¬3å¼æ°: 対象ã®ã¬ã¤ã¤ã¼ Collider[] hitColliders = Physics.OverlapSphere (myTransform.position, radius, raycastLayer); if (hitColliders.Length > 0) { int randomInt = Random.Range(0, hitColliders.Length); targetTransform = hitColliders[randomInt].transform; } } //Playerã¯åå¾ãã¦ãããBox Colliderãéã¢ã¯ãã£ãã®æ = isDeadãtrueã®æ if(targetTransform != null && targetTransform.GetComponent<BoxCollider>().enabled == false){ targetTransform = null; } } void MoveForTarget () { //Playerãªãã¸ã§ã¯ãåå¾æ¸ã¿ã§ãèªåããµã¼ãã¼ã®æ if (targetTransform != null && isServer) { SetNavDestination(targetTransform); } } void SetNavDestination (Transform dest) { //ã¾ã³ãAIã®ç®çå°è¨å® agent.SetDestination(dest.position); } }
Zombie_Target.cs
FixedUpdateã¡ã½ããå ã§ã2ã¤ã®ã¡ã½ãããå¼ã³åºãã¦ãã¾ãã
1ã¤ç®ã¯SearchForTargetã¡ã½ããã§ãPlayerãæ¢ãã¦ãã¾ãã
ã¾ãã¯èªåããµã¼ãã¼ã§ç¡ãå ´åã¯ã¡ã½ãããæãã¾ãã
ç¶ãã¦OverlapSphereã使ããã¾ã³ãã®ä½ç½®ããradiusãåå¾ã¨ããçãä½ãããã®ç¯å²å ã®Playerãªãã¸ã§ã¯ããåå¾ãã¾ãã
åå¾ã§ããããè¤æ°åå¾ããå ´åã¯Player1ä½ã«çµãè¾¼ã¿ã¾ãã
åå¾ããPlayerãªãã¸ã§ã¯ãã®BoxColliderãéã¢ã¯ãã£ãã®æãã¤ã¾ã殺ããã¦isDeadãtrueã«ãªã£ãæã¯ãã¿ã¼ã²ããããå¤ãã¾ãã
2ã¤ç®ã¯MoveForTargetã¡ã½ããã§ãã
Playerãªãã¸ã§ã¯ãåå¾æ¸ã¿ã§èªåããµã¼ãã¼ã®æãSetNavDestinationã¡ã½ãããå¼ã³ãAIãé²ãç®çå°ã¨ãã¦Playerã®ä½ç½®ãæå®ããã¾ãã
â¢ã¾ã³ããçºçããããã¤ã³ããä½æ
ã¾ã³ããçºçããããã¤ã³ããä½ã£ã¦ããã¾ããMainã·ã¼ã³ã§ãHierarchyãã¥ã¼ããCreate > Create Emptyãé¸æããååããZombieSpawnãã«å¤æ´ãã¦ä¸ããã
Position.yã ã1ã«å¤æ´ãã¦ããã¾ãã
ç¶ãã¦Zombieãã¬ããã«å ã»ã©ä½æããZombie_Targetã¹ã¯ãªãããåãä»ãã¦ä¸ãããããã¨ãNetworkç³»ã®ã¹ã¯ãªããã®ããèªåçã«Network Identityã³ã³ãã¼ãã³ããåãä»ãããã¾ãã
ã¤ãã§ã«Add Component > Network > Network Transformãåãä»ãã¦ä¸ããã
ã¾ããRotation Axisã®ã¿ãY (Top-Down 2D)ãã«å¤æ´ãã¦ããã¦ä¸ããã
Network Transformãåãä»ãã
â£ã¾ã³ãçæç¨ã¹ã¯ãªãããä½æ
ZombieSpawnãã§ããã®ã§ãããããã¾ã³ããçºçãããã¹ã¯ãªãããä½ã£ã¦ããã¾ãã
ä»åã¯GameManagerã«ã¤ãã¦ããã®ã§ããGameManager_ZombieSpawnerãã¨ããååã«ãã¾ãã
using UnityEngine; using System.Collections; using UnityEngine.Networking; public class GameManager_ZombieSpawner : NetworkBehaviour { [SerializeField] GameObject zombiePrefab; [SerializeField] GameObject zombieSpawn; private int counter; private int numberOfZombie = 10; public override void OnStartServer () { for (int i = 0; i < numberOfZombie; i++) { SpawnZombies(); } } void SpawnZombies() { //counter(å¾ã»ã©ä½ããå®è£ ) GameObject go = GameObject.Instantiate(zombiePrefab, zombieSpawn.transform.position, Quaternion.identity) as GameObject; NetworkServer.Spawn(go); } // Use this for initialization void Start () { } // Update is called once per frame void Update () { } }
GameManager_ZombieSpawner.cs
ã¾ãã¯GameManagerãªãã¸ã§ã¯ãã«åãä»ãã¾ãã
ããã¨ãå
ç¨ã¨åæ§ã«Network Identityãèªåçã«ä»ãã®ã確èªãã¦ä¸ããã
SerializeFieldã®å¤æ°2ã¤ããInspectorãã¥ã¼ããæå®ãã¦ããã¾ãããã
ãã©ãã°ï¼ããããï¼
Hostãµã¼ãã¼ããã°ã¤ã³ããæã«OnStartServerã¡ã½ãããå®è¡ãããSpawnZombiesã¡ã½ããã10åç¹°ãè¿ããã¾ãã
SpawnZombiesã¡ã½ããã§ã¯zombiePrefabãInstantiateã«ãã£ã¦çæããNetworkServer.Spawnã¡ã½ããã«ã¦ã¾ã³ããå ¨ã¦ã®ã¯ã©ã¤ã¢ã³ãã¨åæãã¾ãã
NetworkServer.Spawnã®å¯¾è±¡ã¨ãªã£ããã¬ããã¯ãNetwork Managerã«ã¦ç»é²ãã¦ãããªããã°ãªãã¾ããã
ã¨ããããã§Menuã·ã¼ã³ãéããNetworkManagerã®Spawn Infoä¸çªä¸ã«ããRegistered Spawnable Prefabã«Zombieãã¬ãããç»é²ãã¦ä¸ããã
ãã©ãã°ï¼ããããï¼
ããã§ã¾ã³ããçºçãã¦ãããã¯ãã§ãã
â¤çµæ
å®è¡ããåã«ZombieSpawnãªãã¸ã§ã¯ãã®ä½ç½®ãé ãã«ãã¦ããã¾ãã
é ãã«é¢ã
ããã§ã¯å®è¡ãã¦ã¿ã¾ãããã
è¿ã
çµæ§é¢ããã¤ããã§ããããã£ã¨ããéã«éåããè©°ãããã¾ããã
ã¨ã«ããã¾ã³ããåºç¾ããããã¨ã«æåãã¾ããï¼
ä»åã¯ããã¾ã§ã§ãã
ãããã¨ããããã¾ããã
ã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ã