###前提・実現したいこと
Unity3Dを使ってTPS視点のアクションゲームを作っています。
プレイヤーと一緒に戦うノンプレイヤーキャラを作っているのですが、一匹目のモンスターを倒したあと
次の行動に移行するまでに時間がかかります。
###発生している問題・エラーメッセージ
2匹めの敵も攻撃するようにりましたが、Update内がうまく言っておりません。
今は、原因究明中です。
###該当のソースコード(2018/01/19改定)
C#
1 [Header("基本設定")] 2 Rigidbody rb; 3 AudioSource aud; 4 5 [Header("対象オブジェクト")] 6 GameObject targetP; 7 public float PlayerLength; //プレイヤーとの設定距離 8 public float PlayerDistance; //プレイヤーとの今の距離 9 10 public Transform target; 11 12 public Transform[] enemyList; //敵のリスト 13 public Transform targetE; //現在選択されている敵 14 public float EnemyDistance; //一番近い敵との距離 15 16 [Header("行動しているか?")] 17 public bool nowExecCoroutine; 18 19 [Header("再攻撃と再移動")] 20 //再攻撃までの時間 21 public float atackInterval = 0; 22 public float atackIntervalMax = 2.0F; 23 24 //再移動までの時間 25 public int moveInterval = 0; 26 public int TukiBunki; 27 28 //アニメーション関係 29 public Animator animator; 30 31 [Header("スクリプト参照")] 32 private EnemyState EState; //EnemyStateスクリプトへの参照 33 private PlayerGuardChecker PGCheck; //PlayerGuardCheckerスクリプトへの参照 34 private NonPcState NPState; 35 36 37 [Header("巡回")] 38 private NavMeshAgent agent; 39 40 // Use this for initialization 41 void Start () { 42 //ターゲットを取得 43 targetP = GameObject.FindGameObjectWithTag("Player"); 44 45 //targetE = GameObject.FindGameObjectWithTag("Enemy"); 46 atackInterval = 0; 47 moveInterval = 200; 48 49 //スクリプト参照 50 EState = GetComponent<EnemyState>(); 51 PGCheck = GetComponent<PlayerGuardChecker>(); 52 53 //音とアニメーション 54 animator = GetComponent<Animator>(); 55 aud = gameObject.GetComponent<AudioSource>(); 56 rb = GetComponent<Rigidbody>(); 57 58 //初期モーション 59 animator.SetBool("Idle", true); 60 61 //NPCの位置チェック 62 //StartCoroutine(NpcPositionCheck()); 63 } 64 65 // Update is called once per frame 66 void Update() 67 { 68 enemyList = GameObject.FindGameObjectsWithTag("Enemy").Select(g => g.transform).OrderBy(g => (g.position - target.position).sqrMagnitude).ToArray(); 69 70 //なんらかの行動をしているか? 71 if (!nowExecCoroutine) 72 { 73 //プレイヤーが近くにいるか? 74 //いる 75 if (PlayerDistance >= 100) 76 { 77 //敵がいない 78 if (enemyList.Length == 0) 79 { 80 if (PlayerDistance > 50) 81 { 82 nowExecCoroutine = true; 83 StartCoroutine(NpcNormalWalk()); 84 Debug.Log("敵がいないのでついていく"); 85 86 } 87 else //if(Vector3.Distance(targetP.transform.position, transform.position) >= 7) 88 { 89 //すごく近いので停止 90 nowExecCoroutine = true; 91 StartCoroutine(NpcNormalKaiten()); 92 Debug.Log("敵がいないので安心の停止"); 93 } 94 } 95 //敵がいる 96 else if (enemyList.Length > 0) 97 { 98 SelectEnemy(0); 99 Debug.Log("tagE" + targetE + "|" + "enemyList0" + enemyList[0]); 100 101 //敵が近くにいるか? 102 //いる 103 if (EnemyDistance >= 80)//80 104 { 105 //敵がいるので抜刀 106 StartCoroutine(NpcKamaeIdle()); 107 Debug.Log("構える"); 108 } 109 //殴れない、近づく 110 else if (EnemyDistance >= 50 && EnemyDistance > 5)//50 111 { 112 nowExecCoroutine = true; 113 StartCoroutine(NpcKamaeWalk()); 114 Debug.Log("敵に近づく"); 115 } 116 else if (EnemyDistance <= 5)//20 117 { 118 nowExecCoroutine = true; 119 StartCoroutine(NpcKamaeTuki()); 120 Debug.Log("敵につき"); 121 } 122 } 123 } 124 //いないが30メートル以内にいるか?) 125 else if (PlayerDistance <= 110) 126 { 127 nowExecCoroutine = true; 128 StartCoroutine(NpcNormalKaiten()); 129 Debug.Log("遠くから見る"); 130 } 131 //いない 132 else 133 { 134 nowExecCoroutine = true; 135 StartCoroutine(NpcNormalIdle()); 136 Debug.Log("待機"); 137 } 138 139 140 141 } 142 } 143 //LateUpdateは、Updateが実行されたあと一回だけ実行される 144 void LateUpdate() 145 { 146 //Find all enemies within range and add them to list. Order list from closest to furtherest 147 //範囲内のすべての敵を見つけてリストに追加します。furtherestに最も近いものからの注文リスト 148 enemyList = GameObject.FindGameObjectsWithTag("Enemy").Select(g => g.transform).OrderBy(g => (g.position - target.position).sqrMagnitude).ToArray(); 149 150 //一番近い敵との距離を算出 151 if (targetE != null) 152 { 153 EnemyDistance = Vector3.Distance(targetE.transform.position, transform.position); 154 } 155 //プレイヤーとの距離を算出 156 PlayerDistance = Vector3.Distance(targetP.transform.position, transform.position); 157 } 158 159 void SelectEnemy(int i) 160 { // 0 = Nearest / New List, 1 = Forwards, -1 = Backwards 161 switch (i) 162 { 163 case 0: 164 targetE = enemyList.FirstOrDefault(); 165 break; 166 default: 167 targetE = enemyList[((System.Array.FindIndex(enemyList, g => g == targetE) + i) % enemyList.Length + enemyList.Length) % enemyList.Length]; 168 break; 169 } 170 Debug.Log("敵がいる" + targetE); 171 } 172 173 public void NpcDead() 174 { 175 StartCoroutine(NpcResurrection()); 176 } 177 178 void moveResaet() 179 { 180 animator.SetBool("Idle", false); 181 animator.SetBool("Walk", false); 182 animator.SetBool("Kamae", false); 183 animator.SetBool("Dead", false); 184 } 185
###試したこと
###補足情報(言語/FW/ツール等のバージョンなど)
【環境】
Unity5.6.1f1 personal
win10
MicrosoftVisualStudio
上記が作成環境になっております。

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/01/19 04:22 編集
2018/01/19 13:19 編集
2018/01/19 13:19