131 lines
4.4 KiB
C#
131 lines
4.4 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using CharacterMovement;
|
|
|
|
// 機器人的 AI : 代替鍵盤餵輸入給 MotionCore 的 PlayerController,物理和玩家完全一樣
|
|
// 行為 : 面向玩家,隨機左右橫移,偶爾停頓、跳躍、滑鏟
|
|
[RequireComponent(typeof(PlayerController))]
|
|
public class BotBrain : MonoBehaviour, ICharacterInputSource
|
|
{
|
|
[Header("目標 (由 BotSpawner 設定)")]
|
|
public Transform target;
|
|
|
|
[Header("活動範圍 (由 BotSpawner 設定)")]
|
|
public Vector3 areaCenter;
|
|
public float areaRadius = 15f;
|
|
|
|
[Header("橫移")]
|
|
[Tooltip("每段橫移的持續時間範圍 (秒)")]
|
|
public Vector2 strafeDuration = new Vector2(0.3f, 1.2f);
|
|
|
|
[Tooltip("每段結束時改成停頓的機率")]
|
|
[Range(0f, 1f)] public float pauseChance = 0.15f;
|
|
|
|
[Header("跳躍 / 滑鏟 (每段開始時判定一次)")]
|
|
[Range(0f, 1f)] public float jumpChance = 0.15f;
|
|
[Range(0f, 1f)] public float slideChance = 0.1f;
|
|
|
|
[Tooltip("滑鏟時按住蹲下的時間 (秒)")]
|
|
public float slideHoldTime = 0.6f;
|
|
|
|
private PlayerController player;
|
|
private Rigidbody body;
|
|
private float segmentTimer;
|
|
private float jumpHeldTimer;
|
|
private float slideTimer;
|
|
|
|
// ===== ICharacterInputSource : MotionCore 每幀讀這些值 =====
|
|
public Vector2 MoveDirection { get; private set; }
|
|
public bool IsMoving => MoveDirection.sqrMagnitude > 0.0001f;
|
|
public bool IsSprinting => slideTimer > 0f; // 滑鏟要在衝刺中才能觸發
|
|
public bool IsWalking => false;
|
|
public bool IsCrouching => slideTimer > 0f;
|
|
public bool IsProne => false;
|
|
public bool IsJumpHeld => jumpHeldTimer > 0f;
|
|
|
|
public event Action JumpPerformed;
|
|
public event Action CrouchPerformed;
|
|
#pragma warning disable 0067 // 介面要求,但機器人不會衝刺技能和趴下
|
|
public event Action DashPerformed;
|
|
public event Action PronePerformed;
|
|
#pragma warning restore 0067
|
|
// ==========================================================
|
|
|
|
void Awake()
|
|
{
|
|
player = GetComponent<PlayerController>();
|
|
body = GetComponent<Rigidbody>();
|
|
|
|
// 朝向由這裡控制 (面向玩家),不讓 MotionCore 把身體轉向移動方向
|
|
player.SetMovementFacingEnabled(false);
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
// PlayerController 在自己的 Start 才設定輸入來源,所以等到 Update 才換成自己
|
|
if (!ReferenceEquals(player.InputSource, this))
|
|
{
|
|
player.SetInputSource(this);
|
|
}
|
|
|
|
float dt = Time.deltaTime;
|
|
if (jumpHeldTimer > 0f) jumpHeldTimer -= dt;
|
|
if (slideTimer > 0f) slideTimer -= dt;
|
|
|
|
segmentTimer -= dt;
|
|
if (segmentTimer <= 0f)
|
|
{
|
|
StartNewSegment();
|
|
}
|
|
}
|
|
|
|
void FixedUpdate()
|
|
{
|
|
// 只轉水平方向面向玩家,移動方向是相對於自己的朝向,所以左右就是對玩家橫移
|
|
if (target == null || body == null) return;
|
|
|
|
Vector3 toTarget = Vector3.ProjectOnPlane(target.position - body.position, Vector3.up);
|
|
if (toTarget.sqrMagnitude > 0.01f)
|
|
{
|
|
body.MoveRotation(Quaternion.LookRotation(toTarget.normalized, Vector3.up));
|
|
}
|
|
}
|
|
|
|
void StartNewSegment()
|
|
{
|
|
segmentTimer = UnityEngine.Random.Range(strafeDuration.x, strafeDuration.y);
|
|
|
|
if (UnityEngine.Random.value < pauseChance)
|
|
{
|
|
MoveDirection = Vector2.zero;
|
|
return;
|
|
}
|
|
|
|
MoveDirection = PickMoveDirection();
|
|
|
|
if (UnityEngine.Random.value < jumpChance)
|
|
{
|
|
jumpHeldTimer = 0.25f; // 按住一下才會跳到完整高度
|
|
JumpPerformed?.Invoke();
|
|
}
|
|
else if (UnityEngine.Random.value < slideChance)
|
|
{
|
|
slideTimer = slideHoldTime; // 先讓 IsSprinting / IsCrouching 成立,再送出蹲下
|
|
CrouchPerformed?.Invoke();
|
|
}
|
|
}
|
|
|
|
// 平常隨機往左或往右 ; 跑出活動範圍時直接往中心走 (用自己朝向的座標表示)
|
|
Vector2 PickMoveDirection()
|
|
{
|
|
Vector3 toCenter = Vector3.ProjectOnPlane(areaCenter - transform.position, Vector3.up);
|
|
if (toCenter.magnitude > areaRadius)
|
|
{
|
|
Vector3 local = transform.InverseTransformDirection(toCenter.normalized);
|
|
return new Vector2(local.x, local.z).normalized;
|
|
}
|
|
|
|
return new Vector2(UnityEngine.Random.value < 0.5f ? -1f : 1f, 0f);
|
|
}
|
|
}
|