今、unityでゲームを作っていて、生成された敵が自分のことを追いかけるようにしたいのですが、うまくいきません。スクリプトは、
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.AI; 5 6public class Enemy_move : MonoBehaviour { 7 8 private Animator animator; 9 private bool aruku=true; 10 private NavMeshAgent agent; 11 public Transform playerTransform;//ここにはプレハブ化したplayerを入れています。 12 [SerializeField] 13 private GameObject player;//ここも同様です。 14 public float ani_speed=1f; 15 16 void Start () 17 { 18 animator = GetComponent<Animator>(); 19 agent = GetComponent<NavMeshAgent>(); 20 animator.SetFloat(name: "speed", value: ani_speed); 21 } 22 23 void OnTriggerStay(Collider other) 24 { 25 if (other.tag=="Player") 26 { 27 aruku = false; 28 } 29 } 30 31 void OnTriggerExit(Collider other) 32 { 33 if (other.tag == "Player") 34 { 35 aruku = true; 36 } 37 } 38 39 public void SetState(bool aruku) 40 { 41 if (aruku == true) 42 { 43 ani_speed = 1f; 44 animator.SetFloat("speed", ani_speed); 45 agent.destination = player.transform.position; 46 if(agent.isStopped == true){ 47 agent.isStopped = false; 48 } 49 } 50 else 51 { 52 ani_speed = 0f; 53 animator.SetFloat("speed", ani_speed); 54 if(agent.isStopped == false){ 55 agent.isStopped = true; 56 } 57 } 58 } 59 void Update() 60 { 61 SetState(aruku: aruku); 62 } 63}
これが敵が追いかけてくるもので、
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Tekiseisei : MonoBehaviour { 6 [SerializeField] 7 private GameObject teki; 8 private Vector3 hassei; 9 private float seconds; 10 // Use this for initialization 11 void Start () { 12 hassei = transform.position; 13 hassei.z -= 4; 14 seconds = 0f; 15 } 16 17 // Update is called once per frame 18 void Update () { 19 seconds += Time.deltaTime; 20 if (seconds >= 7) { 21 GameObject tekis = GameObject.Instantiate(teki) as GameObject; 22 tekis.transform.position = hassei; 23 seconds = 0f; 24 } 25 } 26}
これが敵の生成に使う物です。よろしくお願いします。
現状でどういう動作をするのかを書いてください。うまくいかない、だけではわかりません

すみません。恐らく、プレハブ化したplayerのプレハブの座標を目標地点にしているのだと思います。

回答2件
あなたの回答
tips
プレビュー