問題事項
NavMeshagentを使ってPlayerを追いかける敵キャラを作ることができました.次にこれをランダム出現させたいと思って,プレハブを作って,ランダム出現のGeneratorスクリプトを空のオブジェクトにアタッチしたところ,出現はするけどPlayerを追跡しなくなってしまいました.
よくみると敵キャラにアタッチされてたControlerスクリプトに設定していたagent(追跡対象)が,プレハブだと設定できなくなっていて,それが原因かと思い,Generatorスクリプトをアタッチした空のオブジェクトにNavMeshAgentの機能をつけて,Generatorスクリプトを以下のように変更してみました.そしたらagentにPlayerを設定することができたのでこれで大丈夫だろう,と思ったのですがそれでも追跡はしてくれませんでした...
何がいけないのでしょうか?
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class ZombiGenerator : MonoBehaviour { public GameObject zonbi1Prefab; public GameObject zonbi2Prefab; public GameObject zonbi3Prefab; float span = 1.0f; float delta = 0; public Transform player; public GameObject target; NavMeshAgent agent; // Use this for initialization void Start () { agent = GetComponent<NavMeshAgent> (); } // Update is called once per frame void Update () { this.delta += Time.deltaTime; if (this.delta > this.span) { this.delta = 0; GameObject item; int dice = Random.Range (1, 11); if (dice == 1) { item = Instantiate (zonbi3Prefab) as GameObject; }else if((dice >= 2) && (dice<4)) { item = Instantiate (zonbi2Prefab) as GameObject; }else { item = Instantiate (zonbi1Prefab) as GameObject; } float x = Random.Range (-10, 11); float z = Random.Range (-5, 34); item.transform.position = new Vector3 (x, player.position.y, z); } agent.destination = target.transform.position; } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/14 09:23