Weapons
This commit is contained in:
1 parent
18f513af38
commit
ac91ce5ca7
36 files changed
+2114
-25
No files matched your search
@@ -0,0 +1,130 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bfef044fbd098624a8fcf6725e922a34
|
||||
@@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using CharacterMovement;
|
||||
|
||||
// 機器人的血量 : 掛在機器人根物件上
|
||||
public class BotHealth : MonoBehaviour
|
||||
{
|
||||
[Header("血量")]
|
||||
public float maxHealth = 500f;
|
||||
|
||||
[Header("死亡")]
|
||||
[Tooltip("機器人模型上的 Animator,死亡時觸發 Die")]
|
||||
public Animator animator;
|
||||
|
||||
[Tooltip("倒下後多久移除 (秒)")]
|
||||
public float destroyDelay = 3f;
|
||||
|
||||
public float Health { get; private set; }
|
||||
public bool IsDead { get; private set; }
|
||||
|
||||
// 給 BotSpawner 用 : 倒下時通知,準備重生下一隻
|
||||
public event Action<BotHealth> Died;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
Health = maxHealth;
|
||||
}
|
||||
|
||||
public void TakeDamage(float amount, BodyPart part)
|
||||
{
|
||||
if (IsDead) return;
|
||||
|
||||
Health = Mathf.Max(0f, Health - amount);
|
||||
if (Health <= 0f)
|
||||
{
|
||||
Die();
|
||||
}
|
||||
}
|
||||
|
||||
void Die()
|
||||
{
|
||||
IsDead = true;
|
||||
|
||||
// 停止移動與 AI
|
||||
if (TryGetComponent(out BotBrain brain)) brain.enabled = false;
|
||||
if (TryGetComponent(out PlayerController controller)) controller.enabled = false;
|
||||
if (TryGetComponent(out Rigidbody body))
|
||||
{
|
||||
body.linearVelocity = Vector3.zero;
|
||||
body.isKinematic = true;
|
||||
}
|
||||
|
||||
// 關掉命中框,倒下的機器人不會擋子彈
|
||||
foreach (Hitbox hitbox in GetComponentsInChildren<Hitbox>())
|
||||
{
|
||||
if (hitbox.TryGetComponent(out Collider col)) col.enabled = false;
|
||||
}
|
||||
|
||||
if (animator != null) animator.SetTrigger("Die");
|
||||
|
||||
Died?.Invoke(this);
|
||||
Destroy(gameObject, destroyDelay);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 451ce8b34d8695641b90837ce06f78d1
|
||||
@@ -0,0 +1,83 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
// 一次只有一隻機器人,倒下後在隨機位置重生下一隻
|
||||
// 掛在場景裡的空物件上,物件的位置就是生成範圍的中心
|
||||
public class BotSpawner : MonoBehaviour
|
||||
{
|
||||
[Header("參照")]
|
||||
[Tooltip("機器人 prefab (要有 BotBrain、BotHealth)")]
|
||||
public BotBrain botPrefab;
|
||||
|
||||
[Tooltip("玩家 (機器人會面向它)")]
|
||||
public Transform player;
|
||||
|
||||
[Header("生成範圍")]
|
||||
[Tooltip("以這個物件為中心的半徑 (m),要比場地小,避免生成在牆裡")]
|
||||
public float spawnRadius = 15f;
|
||||
|
||||
[Tooltip("離玩家的最小距離 (m)")]
|
||||
public float minDistanceFromPlayer = 8f;
|
||||
|
||||
[Header("重生")]
|
||||
[Tooltip("倒下後多久生成下一隻 (秒)")]
|
||||
public float respawnDelay = 1.5f;
|
||||
|
||||
void Start()
|
||||
{
|
||||
Spawn();
|
||||
}
|
||||
|
||||
void Spawn()
|
||||
{
|
||||
if (botPrefab == null) return;
|
||||
|
||||
BotBrain bot = Instantiate(botPrefab, PickSpawnPoint(), Quaternion.identity);
|
||||
bot.target = player;
|
||||
bot.areaCenter = transform.position;
|
||||
bot.areaRadius = spawnRadius;
|
||||
|
||||
if (bot.TryGetComponent(out BotHealth health))
|
||||
{
|
||||
health.Died += OnBotDied;
|
||||
}
|
||||
}
|
||||
|
||||
void OnBotDied(BotHealth bot)
|
||||
{
|
||||
bot.Died -= OnBotDied;
|
||||
StartCoroutine(RespawnAfterDelay());
|
||||
}
|
||||
|
||||
IEnumerator RespawnAfterDelay()
|
||||
{
|
||||
yield return new WaitForSeconds(respawnDelay);
|
||||
Spawn();
|
||||
}
|
||||
|
||||
// 在範圍內隨機選點,盡量離玩家遠一點 ; 試幾次都太近就用最後一次的結果
|
||||
Vector3 PickSpawnPoint()
|
||||
{
|
||||
Vector3 point = transform.position;
|
||||
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
Vector2 offset = Random.insideUnitCircle * spawnRadius;
|
||||
point = transform.position + new Vector3(offset.x, 0f, offset.y);
|
||||
|
||||
if (player == null || Vector3.Distance(point, player.position) >= minDistanceFromPlayer)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return point;
|
||||
}
|
||||
|
||||
// 在 Scene 視窗畫出生成範圍,方便調整
|
||||
void OnDrawGizmosSelected()
|
||||
{
|
||||
Gizmos.color = Color.yellow;
|
||||
Gizmos.DrawWireSphere(transform.position, spawnRadius);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ffa4aa80237c6424cbefd7a1db794702
|
||||
@@ -0,0 +1,43 @@
|
||||
using UnityEngine;
|
||||
|
||||
public enum BodyPart
|
||||
{
|
||||
Head,
|
||||
Body,
|
||||
Leg
|
||||
}
|
||||
|
||||
// 掛在機器人骨頭上的命中框 (Collider 要勾 Is Trigger)
|
||||
public class Hitbox : MonoBehaviour
|
||||
{
|
||||
[Header("部位")]
|
||||
public BodyPart part = BodyPart.Body;
|
||||
|
||||
private BotHealth owner;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
owner = GetComponentInParent<BotHealth>();
|
||||
}
|
||||
|
||||
// 部位傷害倍率 : 頭 ×1.5、身體 ×1、腿 ×0.7
|
||||
public static float GetMultiplier(BodyPart part)
|
||||
{
|
||||
switch (part)
|
||||
{
|
||||
case BodyPart.Head: return 1.5f;
|
||||
case BodyPart.Leg: return 0.7f;
|
||||
default: return 1f;
|
||||
}
|
||||
}
|
||||
|
||||
// 回傳實際造成的傷害 (機器人已經倒下就回傳 0)
|
||||
public float Hit(float baseDamage)
|
||||
{
|
||||
if (owner == null || owner.IsDead) return 0f;
|
||||
|
||||
float damage = baseDamage * GetMultiplier(part);
|
||||
owner.TakeDamage(damage, part);
|
||||
return damage;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a53cad675bcc11640ae35481c28700e7
|
||||
Reference in new issue
Block a user