スクリプトの整理

This commit is contained in:
oogushiyuuga
2026-06-29 17:23:26 +09:00
parent 4d1dc01b62
commit e8bdb0920d
6 changed files with 74 additions and 123 deletions

View File

@@ -16,30 +16,23 @@ public class EnemyAI : MonoBehaviour
Stun // 怯み
}
[Header("現在の状態")]
// 現在の状態
[SerializeField] private AIState currentState = AIState.Wander;
[Header("索敵設定")]
// 索敵設定
[SerializeField] private float detectionRange = 10f;
[SerializeField] private float loseRange = 15f;
[SerializeField] private string playerTag = "Player";
[Header("攻撃設定")]
[Tooltip("この距離に近づいたら攻撃 (メートル)")]
// 攻撃設定
[SerializeField] private float attackRange = 1.5f;
[Tooltip("パンチの攻撃力")]
[SerializeField] private float attackDamage = 10f;
[Tooltip("一度攻撃が終了してから、次の攻撃を開始するまでの待ち時間 (秒)")]
[SerializeField] private float attackCooldown = 2.0f;
[Tooltip("攻撃の予兆時間 (秒)")]
[SerializeField] private float telegraphDuration = 0.6f;
[SerializeField] private float telegraphDuration = 0.6f; //攻撃ため時間
[SerializeField] private Collider rightHandCollider;
[SerializeField] private float comboInterval = 0.2f; // 連続攻撃のインターバル
[Header("ループ攻撃の調整")]
[Tooltip("連続攻撃の間に挟む最低限のインターバル (秒)")]
[SerializeField] private float comboInterval = 0.2f;
[Header("徘徊Wander設定")]
// 索敵設定
[SerializeField] private float wanderRadius = 8f;
[SerializeField] private float minWaitTime = 2f;
[SerializeField] private float maxWaitTime = 4f;
@@ -77,7 +70,7 @@ public class EnemyAI : MonoBehaviour
UpdateRotationOnAttack();
}
#region AI ()
// AI メインループ (思考ロジック)
private IEnumerator AILoop()
{
while (true)
@@ -120,10 +113,8 @@ public class EnemyAI : MonoBehaviour
playerTransform = playerObj.transform;
}
}
#endregion
#region
/// 【徘徊状態】の思考ロジック
// 徘徊状態の思考ロジック
private void WanderBehavior()
{
if (playerTransform != null)
@@ -135,7 +126,7 @@ public class EnemyAI : MonoBehaviour
currentState = AIState.Chase;
isWandering = false;
agent.ResetPath();
Debug.Log($"👁️ {gameObject.name}: プレイヤーを発見 追跡します。");
Debug.Log($"{gameObject.name}: プレイヤーを発見追跡します。");
return;
}
}
@@ -147,7 +138,7 @@ public class EnemyAI : MonoBehaviour
}
}
/// 追跡状態の思考ロジック
// 追跡状態の思考ロジック
private void ChaseBehavior()
{
if (playerTransform == null)
@@ -171,7 +162,7 @@ public class EnemyAI : MonoBehaviour
{
currentState = AIState.Wander;
agent.ResetPath();
Debug.Log($"{gameObject.name}: プレイヤーを見失った。");
Debug.Log($"{gameObject.name}: プレイヤーを見失った。");
return;
}
@@ -182,7 +173,7 @@ public class EnemyAI : MonoBehaviour
}
}
/// 攻撃状態の思考ロジック
// 攻撃状態の思考ロジック
private void AttackBehavior()
{
if (playerTransform == null)
@@ -200,16 +191,14 @@ public class EnemyAI : MonoBehaviour
return;
}
// クールダウン中でなければ攻撃コルーチンを実行クールダウン中の場合は、Update内の処理によりプレイヤーの方向を向きながら待機します
// クールダウン中でなければ攻撃コルーチンを実行クールダウン中の場合は、Update内の処理によりプレイヤーの方向を向きながら待機
if (!isCooldown)
{
StartCoroutine(PerformPunchAttack());
}
}
#endregion
#region
/// エージェントの移動状態に応じてAnimatorのSpeedパラメータを更新する
// エージェントの移動状態に応じてAnimatorのSpeedパラメータを更新する
private void UpdateAnimation()
{
float targetSpeed = 0f;
@@ -224,7 +213,7 @@ public class EnemyAI : MonoBehaviour
anim.SetFloat("Speed", targetSpeed, 0.1f, Time.deltaTime);
}
/// 攻撃ステートの際、プレイヤーの方向へ滑らかに旋回させる
// 攻撃ステートの際、プレイヤーの方向へ滑らかに旋回させる
private void UpdateRotationOnAttack()
{
if (currentState == AIState.Attack && playerTransform != null && !isKnockbacking && currentState != AIState.Stun)
@@ -239,7 +228,6 @@ public class EnemyAI : MonoBehaviour
}
}
}
#endregion
// ダメージを受けた際に、EnemyhHealthから呼び出されるのけぞり開始メソッド
public void TriggerKnockback(float duration)
@@ -337,8 +325,7 @@ public class EnemyAI : MonoBehaviour
StartCoroutine(AILoop());
}
#region
/// 徘徊時のランダム移動を制御するコルーチン
// 徘徊時のランダム移動を制御するコルーチン
private IEnumerator WanderMoveRoutine()
{
isWandering = true;
@@ -388,14 +375,14 @@ public class EnemyAI : MonoBehaviour
isWandering = false;
}
/// 予兆から攻撃アニメーションのトリガーまでを制御するコルーチン
// 予兆から攻撃アニメーションのトリガーまでを制御するコルーチン
private IEnumerator PerformPunchAttack()
{
isAttacking = true;
isCooldown = true; // ここで攻撃フラグをロックします
isCooldown = true; // ここで攻撃フラグをロック
anim.SetTrigger("Telegraph");
Debug.Log($"⚠️ {gameObject.name}: 攻撃の予兆(溜め開始");
Debug.Log($"{gameObject.name}: 攻撃の溜め開始");
yield return new WaitForSeconds(telegraphDuration);
@@ -404,25 +391,23 @@ public class EnemyAI : MonoBehaviour
anim.SetTrigger("Attack");
}
/// 連続攻撃成功時の、クイックなクールダウン解除処理
// 連続攻撃成功時の、クイックなクールダウン解除処理
private IEnumerator ResetCooldownQuickly()
{
yield return new WaitForSeconds(comboInterval);
isCooldown = false;
}
/// コンボが途切れた際の、通常の攻撃クールダウン解除処理
// コンボが途切れた際の、通常の攻撃クールダウン解除処理
private IEnumerator ResetCooldown()
{
// 指定された秒数待機する
yield return new WaitForSeconds(attackCooldown);
isCooldown = false;
Debug.Log($"{gameObject.name}: 攻撃クールダウンが終了しました。再攻撃可能です。");
Debug.Log($"{gameObject.name}: 攻撃クールダウンが終了しました。再攻撃可能です。");
}
#endregion
#region
/// 攻撃アニメーションのヒットフレームで実行されるヒット判定Animation Eventから呼び出し
// 攻撃アニメーションのヒットフレームで実行されるヒット判定Animation Eventから呼び出し
public void OnPunchHit()
{
if (isKnockbacking || currentState == AIState.Stun) return;
@@ -430,7 +415,7 @@ public class EnemyAI : MonoBehaviour
SetFistCollider(true);
}
/// Recovery硬直アニメーションの終了時に実行される処理Animation Eventから呼び出し
// Recovery硬直アニメーションの終了時に実行される処理Animation Eventから呼び出し
public void OnRecoveryEnd()
{
if(isKnockbacking || currentState == AIState.Stun) return;
@@ -451,7 +436,7 @@ public class EnemyAI : MonoBehaviour
if (distanceToPlayer <= attackRange)
{
StartCoroutine(ResetCooldownQuickly());
Debug.Log($"🔄 {gameObject.name}: プレイヤーがまだ射程内です。連続攻撃(コンボ)を実行します。");
Debug.Log($"{gameObject.name}: プレイヤーがまだ射程内です。連続攻撃を実行します。");
}
else
{
@@ -461,10 +446,8 @@ public class EnemyAI : MonoBehaviour
Debug.Log($"🏃 {gameObject.name}: プレイヤーが離れたため追跡に戻ります。({attackCooldown}秒の攻撃クールダウンを適用)");
}
}
#endregion
#region
/// 指定された中心座標の範囲内から、NavMesh上の有効なランダム座標を取得する
// 指定された中心座標の範囲内から、NavMesh上の有効なランダム座標を取得する
private void SetFistCollider(bool enabledState)
{
@@ -485,8 +468,8 @@ public class EnemyAI : MonoBehaviour
}
return center;
}
/// エディタのSceneビューに索敵・攻撃範囲をギズモとして描画する
// エディタのSceneビューに索敵・攻撃範囲をギズモとして描画する
private void OnDrawGizmosSelected()
{
// 索敵範囲(赤)
@@ -501,5 +484,4 @@ public class EnemyAI : MonoBehaviour
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(transform.position, attackRange);
}
#endregion
}