【Unity】敵がプレイヤーに気づいて追尾する #2

投稿者: | 2020-10-11

敵が走っているプレイヤーに気づいて追尾するでは、プレイヤーが走ると注意レベルのゲージが上がって、満タンになると敵がプレイヤーを追尾しはじめます。

走るのをやめるとゲージが下がって0になると追尾をやめますが、敵にプレイヤーが見えていなくて音で判断していると仮定すると、歩いていて足音が大きくなくても追ってくるのはおかしいと思ったので修正してみました。

敵とプレイヤーの距離も考えないようにしています。

動画では、敵に、巡回している・追っている・見失っている の3つの状態を用意して、満タンの1になると追っている状態になり、1未満になると見失っている状態にして、その後プレイヤーを見失った位置まで来るか、ゲージが0.4以下になると巡回に戻るようにしています。

  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.AI;
  4.  
  5.  
  6. public class AgentScript3 : MonoBehaviour
  7. {
  8. [SerializeField] UnityStandardAssets.Characters.FirstPerson.FirstPersonController sc_fps;
  9. [SerializeField] Transform points;
  10. [SerializeField] Image gauge;
  11. [SerializeField] Text text;
  12. [SerializeField] GameObject spherePrefab;
  13.  
  14. GameObject sphere;
  15. Transform player;
  16. NavMeshAgent agent;
  17. float attention;
  18. int state;
  19.  
  20. // Start is called before the first frame update
  21. void Start()
  22. {
  23. player = sc_fps.transform;
  24. agent = GetComponent<NavMeshAgent>();
  25.  
  26. Vector3 dest = GetDestinationRandomly();
  27. agent.destination = dest;
  28. sphere = Instantiate(spherePrefab, dest, spherePrefab.transform.rotation);
  29.  
  30. gauge.fillAmount = 0f;
  31. }
  32.  
  33. // Update is called once per frame
  34. void Update()
  35. {
  36. // プレイヤーが走っていないとき
  37. if (sc_fps.GetIsWalking())
  38. {
  39. text.text = "P:走っていない";
  40. attention -= 0.008f; // 注意のレベルを下げる
  41. }
  42. // プレイヤーが走っているとき
  43. else
  44. {
  45. text.text = "P:走っている";
  46. attention += 0.01f; // 注意のレベルを上げる
  47. }
  48.  
  49. // 追っていない状態
  50. if (state == 0)
  51. {
  52. //
  53. if (attention <= 0f)
  54. {
  55. attention = 0f;
  56. }
  57.  
  58. text.text += "\nA:巡回している";
  59.  
  60. // 1以上のとき追っている状態にする
  61. if (attention >= 1f)
  62. {
  63. attention = 1f;
  64. state = 1;
  65. agent.destination = player.position; // プレイヤーの場所に行く
  66. Destroy(sphere);
  67. sphere = Instantiate(spherePrefab, player.position, spherePrefab.transform.rotation);
  68. }
  69. // 目的地付近で次の目的地を設定
  70. else if (!agent.pathPending && agent.remainingDistance < 0.5f)
  71. {
  72. Vector3 dest = GetDestinationRandomly();
  73. agent.destination = dest;
  74. Destroy(sphere);
  75. sphere = Instantiate(spherePrefab, dest, spherePrefab.transform.rotation);
  76.  
  77. }
  78.  
  79. }
  80. // 追っている状態
  81. else if (state == 1)
  82. {
  83.  
  84. text.text += "\nA:追っている";
  85. agent.destination = player.position; // プレイヤーの場所に行く
  86. Destroy(sphere);
  87. sphere = Instantiate(spherePrefab, player.position, spherePrefab.transform.rotation);
  88.  
  89. // 1以上のとき1
  90. if (attention >= 1f)
  91. {
  92. attention = 1f;
  93. }
  94. // 見失っている状態にする
  95. else
  96. {
  97. state = 2;
  98. }
  99. }
  100. // 見失っている状態
  101. else if (state == 2)
  102. {
  103. text.text += "\nA:見失っている";
  104.  
  105. // 1以上のとき追っている状態にする
  106. if (attention >= 1f)
  107. {
  108. attention = 1f;
  109. state = 1;
  110. agent.destination = player.position; // プレイヤーの場所に行く
  111. Destroy(sphere);
  112. sphere = Instantiate(spherePrefab, player.position, spherePrefab.transform.rotation);
  113. }
  114. // 0.4で追ってない状態にする
  115. else if (attention <= 0.4f)
  116. {
  117. Vector3 dest = GetDestinationRandomly();
  118. agent.destination = dest;
  119. Destroy(sphere);
  120. sphere = Instantiate(spherePrefab, dest, spherePrefab.transform.rotation);
  121. state = 0;
  122. }
  123. // 目的地付近で次の目的地
  124. else if (!agent.pathPending && agent.remainingDistance < 0.5f)
  125. {
  126. Vector3 dest = GetDestinationRandomly();
  127. agent.destination = dest;
  128. Destroy(sphere);
  129. sphere = Instantiate(spherePrefab, dest, spherePrefab.transform.rotation);
  130. state = 0;
  131. }
  132. }
  133.  
  134. gauge.fillAmount = attention; // ゲージに反映
  135.  
  136. }
  137.  
  138. // ランダムで目的地を返す
  139. Vector3 GetDestinationRandomly()
  140. {
  141. return points.GetChild(Random.Range(0, points.childCount)).transform.position;
  142. }
  143. }

まず前の記事と同様にFirstPersonControllerに、歩いているかどうかを表すbool変数のGetメソッドを作って、それを使ってプレイヤーが走っているか調べて、注意のレベルを上げ下げします。

  1. // プレイヤーが走っていないとき
  2. if (sc_fps.GetIsWalking())
  3. {
  4. text.text = "P:走っていない";
  5. attention -= 0.008f; // 注意のレベルを下げる
  6. }
  7. // プレイヤーが走っているとき
  8. else
  9. {
  10. text.text = "P:走っている";
  11. attention += 0.01f; // 注意のレベルを上げる
  12. }

その後は、敵の状態によって条件分岐して、その中で、追っているときはプレイヤーの位置を目的地に入れ続けるとか、注意のレベルが0~1の間に収まるようにするなどの処理をしたり、条件によって他の状態に遷移させたりしているだけです。

目的地には緑色のSphereを配置しています。生成するときに前のを破壊するので、常に1つしか見えません。

巡回しているときは、目的地に来ると次の目的地をランダムで設定します。

また、プレイヤーが走ってゲージが1以上になると巡回をやめて追っている状態になり、目的地がプレイヤーの位置になります。

追っている状態のときは、毎フレーム目的地をプレイヤーの位置に更新しつづけます。

プレイヤーが走るのをやめると、目的地の更新をやめるので、緑のSphereがプレイヤーについてこなくなり、敵はプレイヤーを見失っている状態になります。

敵はとりあえず見失った位置に向かいますが、ゲージが0.4以下になると、あきらめて巡回に戻ります。

見失ってすぐにそこへ敵が到達すると、ゲージが半分以上残っていても巡回に戻るようにしました。

次はプレイヤーが敵に見えているかどうかや、敵とプレイヤーの距離もゲージに反映させてみます。

コメントを残す

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