【Unity】プレイヤーと同じ階のナビメッシュ上の点を目的地に設定する

投稿者: | 2021-02-26

建物の床などナビメッシュが上下に重なってしまうところでは、プレイヤーがいる階のナビメッシュ上の点がただしく取れないときがあるので、階を判別して目的地を探す方法を変えてみました。

まず、階ごとに別々のエリアを設定します。

例えば1階の床のPlaneでは、Navigation Staticのチェックを入れて、Navigation Areaを1階にしました。

プレイヤーのスクリプトでは、WASDでの移動と、適当に高さによる階の判別をしています。

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class TestPlayer : MonoBehaviour
  7. {
  8. [SerializeField] Text text;
  9. [SerializeField] float speed = 0.05f; // 移動速度
  10. public int Floor { get; private set; } // プレイヤーのいる階
  11. // Start is called before the first frame update
  12. void Start()
  13. {
  14. text.text = "";
  15.  
  16. }
  17.  
  18. // Update is called once per frame
  19. void Update()
  20. {
  21. // WASDで移動
  22. if (Input.anyKey)
  23. {
  24. Vector3 dir = Vector3.zero;
  25. if (Input.GetKey(KeyCode.A))
  26. {
  27.  
  28. dir.x -= 1;
  29.  
  30.  
  31. }
  32. if (Input.GetKey(KeyCode.D))
  33. {
  34.  
  35. dir.x += 1;
  36.  
  37.  
  38. }
  39. if (Input.GetKey(KeyCode.W))
  40. {
  41.  
  42. dir.z += 1;
  43.  
  44. }
  45. if (Input.GetKey(KeyCode.S))
  46. {
  47.  
  48. dir.z -= 1;
  49.  
  50. }
  51. if(Input.GetKey(KeyCode.LeftShift))
  52. {
  53. dir.y += 1;
  54. }
  55. if(Input.GetKey(KeyCode.LeftControl))
  56. {
  57. dir.y -= 1;
  58. }
  59.  
  60. dir = Vector3.Normalize(dir);
  61.  
  62. Vector3 pos = transform.position;
  63. pos = pos + dir * speed;
  64. transform.position = pos;
  65. }
  66.  
  67. // プレイヤーがいる階を判定
  68. if (transform.position.y < 2.5f)
  69. {
  70.  
  71. Floor = 1;
  72. text.text = $"{Floor}階";
  73.  
  74. }
  75. else if (transform.position.y >= 13.77f)
  76. {
  77. Floor = 7;
  78. }
  79. else if (transform.position.y >= 11.36f)
  80. {
  81. Floor = 6;
  82. }
  83. else if (transform.position.y >= 9.21f)
  84. {
  85. Floor = 5;
  86. }
  87. else if (transform.position.y >= 7.06f)
  88. {
  89. Floor = 4;
  90. }
  91. else if (transform.position.y >= 4.9f)
  92. {
  93. Floor = 3;
  94. text.text = $"{Floor}階";
  95. }
  96. else if (transform.position.y >= 2.5f)
  97. {
  98. Floor = 2;
  99. text.text = $"{Floor}階";
  100.  
  101. }
  102.  
  103. }
  104. }

エージェントのいる階の判別ではコライダーを使いました。コライダーはトリガーにして、Rigidbodyを付けます。

そして、エージェントのスクリプトで目的地を設定するときに、プレイヤーとエージェントが同じ階にいるときには、エリアマスクをかけてからプレイヤーのいる地点を目的地に設定し、別の階にいるときはNavMesh.SamplePositionメソッドを使って、プレイヤーのいる点から最も近いナビメッシュ上の点をサンプルするようにしました。

  1.  
  2. using UnityEngine;
  3. using UnityEngine.AI;
  4. using UnityEngine.UI;
  5.  
  6. public class TestAgent3 : MonoBehaviour
  7. {
  8. [SerializeField] float maxDistance; // サンプルする範囲
  9. public int Floor { get; private set; } = 1; // エージェントのいる階
  10. NavMeshAgent agent;
  11. [SerializeField] Text text;
  12. [SerializeField] Text centerText;
  13. [SerializeField] GameObject cube; // 目印のCubeのプレハブ
  14.  
  15. // Start is called before the first frame update
  16. void Start()
  17. {
  18. agent = GetComponent<NavMeshAgent>();
  19. text.text = $"{Floor}階";
  20. }
  21.  
  22. // Update is called once per frame
  23. void Update()
  24. {
  25. if (Input.GetKeyDown(KeyCode.E))
  26. {
  27. // 目的地を設定
  28. SetDestination();
  29. }
  30. }
  31.  
  32. // 目印のCubeを置く
  33. void DisplayPosition(Vector3 pos)
  34. {
  35. Destroy(GameObject.FindGameObjectWithTag("Cube"));
  36. Instantiate(cube, pos, Quaternion.identity);
  37. }
  38.  
  39. void SetDestination()
  40. {
  41. // プレイヤーのスクリプトを首都行く
  42. GameObject player = GameObject.FindGameObjectWithTag("Player");
  43. TestPlayer playerScript = player.GetComponent<TestPlayer>();
  44. // 同じ階にいるか、エージェントが階段にいるとき
  45. if (playerScript.Floor == Floor || Floor == -1)
  46. {
  47. // エリアマスクを設定
  48. SetAreaMask(playerScript.Floor);
  49.  
  50. // プレイヤーの現在位置を目的地に設定
  51. agent.SetDestination(player.transform.position);
  52. centerText.text = "目的地を設定";
  53.  
  54. // 目印を置く
  55. Vector3 pos = player.transform.position;
  56. pos.y = playerScript.Floor == 1 ? 0f : 2.52f;
  57. DisplayPosition(pos);
  58. }
  59. // 別の階にいるとき
  60. else
  61. {
  62. // エリアマスクを設定
  63. SetAllAreas();
  64.  
  65. // 最寄りのナビメッシュ上の点を目的地に設定
  66. centerText.text = "位置をサンプル";
  67. NavMeshHit hit;
  68. if (NavMesh.SamplePosition(player.transform.position, out hit, maxDistance, GetAreaMask(playerScript.Floor)))
  69. {
  70.  
  71. agent.destination = hit.position;
  72.  
  73. // 目印を置く
  74. DisplayPosition(hit.position);
  75. }
  76. }
  77.  
  78. }
  79.  
  80. // 設定するエリアマスク
  81. int GetAreaMask(int floor)
  82. {
  83. if (floor == 1)
  84. {
  85.  
  86. return 1 << 3 | 1 << 5;
  87.  
  88. }
  89. else if (floor == 2)
  90. {
  91.  
  92. return 1 << 4 | 1 << 5;
  93.  
  94. }
  95. else if (floor == 3)
  96. {
  97.  
  98. return 1 << 6;
  99.  
  100. }
  101. else if (floor == 4)
  102. {
  103.  
  104. return 1 << 7;
  105.  
  106. }
  107. else if (floor == 5)
  108. {
  109.  
  110. return 1 << 8;
  111.  
  112. }
  113. else if (floor == 6)
  114. {
  115.  
  116. return 1 << 9;
  117.  
  118. }
  119. else if (floor == 7)
  120. {
  121.  
  122. return 1 << 10;
  123. }else
  124. {
  125. return NavMesh.AllAreas;
  126. }
  127. }
  128.  
  129. int GetAreaMask()
  130. {
  131. return agent.areaMask;
  132. }
  133.  
  134. // エリアマスクを設定
  135. public void SetAreaMask(int floor)
  136. {
  137.  
  138. agent.areaMask = GetAreaMask(floor);
  139.  
  140. }
  141.  
  142. public void SetAllAreas()
  143. {
  144. agent.areaMask = NavMesh.AllAreas;
  145. }
  146.  
  147. // コライダーを出たとき
  148. private void OnTriggerExit(Collider other)
  149. {
  150. if(other.name == "Collider1F")
  151. {
  152. // 1階のコライダーを階段の方へ出る
  153. if(transform.position.x >= other.transform.position.x)
  154. {
  155. Floor = -1;
  156. text.text = "階段";
  157. }
  158. // 1階のコライダーを1階の方へ出る
  159. else
  160. {
  161. Floor = 1;
  162. text.text = $"{Floor}階";
  163.  
  164. }
  165.  
  166. }
  167. else if(other.name == "Collider2F")
  168. {
  169. // 1階のコライダーを階段の方へ出る
  170. if (transform.position.x >= other.transform.position.x)
  171. {
  172. Floor = -1;
  173. text.text = "階段";
  174.  
  175. }
  176. // 1階のコライダーを2階の方へ出る
  177. else
  178. {
  179. Floor = 2;
  180. text.text = $"{Floor}階";
  181. }
  182. }
  183. }
  184.  
  185. }

同じ階にいるときに他の階にいけるようにすると、プレイヤーがナビメッシュ上にいないときに他の階に目的地が設定されるので注意です。それを避けるためにプレイヤーの階と階段だけのエリアマスクを付けました。

位置をサンプルするときはエージェントのエリアマスクはすべてのエリアに設定しますが、サンプルするエリアはプレイヤーがいる階だけにします。同じ階のナビメッシュ上の点を探す

ちなみに、別の階にいるときにエージェントのエリアマスクをプレイヤーがいる階だけに設定すると、エージェントはその階へ瞬間移動してしまいました。

コメントを残す

メールアドレスが公開されることはありません。