質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
HLSL

HLSLは、米マイクロソフト社によって開発された Direct3D APIで使われるプロプライエタリなシェーディング言語です。

Q&A

0回答

1309閲覧

[Unity3D] NavMeshAgentで目的地との距離を取得する正しい方法が知りたい。

退会済みユーザー

退会済みユーザー

総合スコア0

HLSL

HLSLは、米マイクロソフト社によって開発された Direct3D APIで使われるプロプライエタリなシェーディング言語です。

0グッド

0クリップ

投稿2021/07/30 01:48

編集2021/07/31 11:53

提示コードのAI()関数部ですがvoid Start();関数部で最初の目的地を設定した後なぜかUpdate();関数内のAI();関数が呼ばれif()文の中身が実行されてしまいます。デバッグログを見ると目的地の距離が0が24回Update()内で起きてるみたいなのですがこの場合どういった手法で目的地との距離を指定するのでしょうか?

######やりたいこと
目的地までの距離を得たい。

参考サイト: https://docs.unity3d.com/ja/2018.4/ScriptReference/AI.NavMeshAgent-destination.html
参考サイト: https://docs.unity3d.com/ja/2019.4/Manual/nav-AgentPatrol.html
参考サイト: https://docs.unity3d.com/ja/2018.4/ScriptReference/AI.NavMeshAgent-remainingDistance.html

cs

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.AI; 5 6public class Tank_A : Enemy_Base 7{ 8 private Vector3 attackVector; //攻撃方向 9 private NavMeshAgent agent; //巡回 10 private int destPoint = 0; 11 [SerializeField] private Transform[] point; //巡回地点 12 [SerializeField] private GameObject bullet; //発射する弾 13 14 15 16 private void Awake() 17 { 18 19 } 20 21 // Start is called before the first frame update 22 void Start() 23 { 24 agent = GetComponent<NavMeshAgent>(); 25 agent.destination = point[destPoint].position; 26 //AI(); 27 } 28 29 // Update is called once per frame 30 void Update() 31 { 32 33 34 35 AI(); 36 } 37 38 private void FixedUpdate() 39 { 40 time += Time.deltaTime; 41 42 43 Debug.Log(agent.remainingDistance); 44 } 45 46 47 void AI() 48 { 49 if (agent.remainingDistance < 0.0001f) 50 { 51 //Debug.Log("あああ"); 52 agent.destination = point[destPoint].position; 53 destPoint = (destPoint + 1) % point.Length; 54 } 55 } 56 57 public override void OnAttackAreaStay(Collider other) 58 { 59 if(other.gameObject.tag == "Player") 60 { 61 attackVector = other.gameObject.transform.position - transform.Find("Turret1").transform.Find("Main Gun1").transform.position; 62 transform.Find("Turret1").rotation = Quaternion.LookRotation(new Vector3(attackVector.x, 0, attackVector.z)); 63 transform.Find("Turret1").transform.Find("Main Gun1").transform.rotation = Quaternion.LookRotation(attackVector); 64 65 if(time >= intervalTime || time == 0) 66 { 67 GameObject g = Instantiate(bullet, transform.Find("Turret1").transform.Find("Main Gun1").transform.position, 68 transform.Find("Turret1").transform.Find("Main Gun1").transform.rotation); 69 g.gameObject.GetComponent<Bullet>().moveVector = attackVector; 70 71 time = 0; 72 } 73 } 74 75 76 } 77 78 79 public override void OnAttackAreaExit(Collider other) 80 { 81 if (other.gameObject.tag == "Player") 82 { 83 84 } 85 } 86 87 88 89} 90

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問