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

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

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

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

1817閲覧

stealthのアセットでプレイヤーを撃ったあと敵が立ったままになってしまいます。

akino3

総合スコア7

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2016/12/22 12:36

###前提・実現したいこと
unity stealthのアセットでプレイヤーを撃ったあと敵が立ったままになってしまいます。
どうすれば、再び歩行してくれるようになるでしょうか。

using UnityEngine;
using System.Collections;

public class DoneEnemyAI : MonoBehaviour
{
public float patrolSpeed = 2f; // The nav mesh agent's speed when patrolling.
public float chaseSpeed = 5f; // The nav mesh agent's speed when chasing.
public float chaseWaitTime = 5f; // The amount of time to wait when the last sighting is reached.
public float patrolWaitTime = 1f; // The amount of time to wait when the patrol way point is reached.
public Transform[] patrolWayPoints; // An array of transforms for the patrol route.

private DoneEnemySight enemySight; // Reference to the EnemySight script. private UnityEngine.AI.NavMeshAgent nav; // Reference to the nav mesh agent. private Transform player; // Reference to the player's transform. private DonePlayerHealth playerHealth; // Reference to the PlayerHealth script. private DoneLastPlayerSighting lastPlayerSighting; // Reference to the last global sighting of the player. private float chaseTimer; // A timer for the chaseWaitTime. private float patrolTimer; // A timer for the patrolWaitTime. private int wayPointIndex; // A counter for the way point array. void Awake () { // Setting up the references. enemySight = GetComponent<DoneEnemySight>(); nav = GetComponent<UnityEngine.AI.NavMeshAgent>(); player = GameObject.FindGameObjectWithTag(DoneTags.player).transform; playerHealth = player.GetComponent<DonePlayerHealth>(); lastPlayerSighting = GameObject.FindGameObjectWithTag(DoneTags.gameController).GetComponent<DoneLastPlayerSighting>(); } void Update () { // If the player is in sight and is alive... if(enemySight.playerInSight && playerHealth.health > 0f) // ... shoot. Shooting(); // If the player has been sighted and isn't dead... else if(enemySight.personalLastSighting != lastPlayerSighting.resetPosition && playerHealth.health > 0f) // ... chase. Chasing(); // Otherwise... else // ... patrol. Patrolling(); } void Shooting () { // Stop the enemy where it is. nav.Stop(); } void Chasing () { // Create a vector from the enemy to the last sighting of the player. Vector3 sightingDeltaPos = enemySight.personalLastSighting - transform.position; // If the the last personal sighting of the player is not close... if(sightingDeltaPos.sqrMagnitude > 4f) // ... set the destination for the NavMeshAgent to the last personal sighting of the player. nav.destination = enemySight.personalLastSighting; // Set the appropriate speed for the NavMeshAgent. nav.speed = chaseSpeed; // If near the last personal sighting... if(nav.remainingDistance < nav.stoppingDistance) { // ... increment the timer. chaseTimer += Time.deltaTime; // If the timer exceeds the wait time... if(chaseTimer >= chaseWaitTime) { // ... reset last global sighting, the last personal sighting and the timer. lastPlayerSighting.position = lastPlayerSighting.resetPosition; enemySight.personalLastSighting = lastPlayerSighting.resetPosition; chaseTimer = 0f; } } else // If not near the last sighting personal sighting of the player, reset the timer. chaseTimer = 0f; } void Patrolling () { // Set an appropriate speed for the NavMeshAgent. nav.speed = patrolSpeed; // If near the next waypoint or there is no destination... if(nav.destination == lastPlayerSighting.resetPosition || nav.remainingDistance < nav.stoppingDistance) { // ... increment the timer. patrolTimer += Time.deltaTime; // If the timer exceeds the wait time... if(patrolTimer >= patrolWaitTime) { // ... increment the wayPointIndex. if(wayPointIndex == patrolWayPoints.Length - 1) wayPointIndex = 0; else wayPointIndex++; // Reset the timer. patrolTimer = 0f; } } else // If not near a destination, reset the timer. patrolTimer = 0f; // Set the destination to the patrolWayPoint. nav.destination = patrolWayPoints[wayPointIndex].position; }

}

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

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

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

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

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

guest

回答1

0

ベストアンサー

if(enemySight.playerInSight && playerHealth.health > 0f)
この条件を満たしている限り(Shooting()が実行され続けるので)停止し続けます。

Chasing()またはPatrolling()が実行されれば動くようになるかと思いますので、
条件分岐が正しく行われているか、
Debug.Log(enemySight.playerInSight + " " +playerHealth.health);
これをUpdateの中に入れて確認してみてください。

投稿2016/12/27 12:41

sakura_hana

総合スコア11427

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

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

akino3

2017/01/02 11:44

回答ありがとうございます。。しばらく、考えた末、私もshooting の項目で止まる事がわかり、 void Shooting () { // Stop the enemy where it is. patrolTimer = 0; とする事で、自己解決してしまいました。 しかし、私の望む、条件分岐まで戻らず悩んでいたので、非常に助かります。 早速試してみます。回答ありがとうございました(*´ω`)
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問