放置障礙物,優化玩家尋找敵方方式
This commit is contained in:
1 parent
f07d687be8
commit
c644491e93
14 files changed
+2517
-26
No files matched your search
@@ -23,6 +23,9 @@ public class BotSpawner : MonoBehaviour
|
||||
[Tooltip("倒下後多久生成下一隻 (秒)")]
|
||||
public float respawnDelay = 1.5f;
|
||||
|
||||
// 目前活著的機器人,倒下到重生之間是 null (給方位指示等 UI 用)
|
||||
public BotHealth CurrentBot { get; private set; }
|
||||
|
||||
void Start()
|
||||
{
|
||||
Spawn();
|
||||
@@ -40,12 +43,14 @@ public class BotSpawner : MonoBehaviour
|
||||
if (bot.TryGetComponent(out BotHealth health))
|
||||
{
|
||||
health.Died += OnBotDied;
|
||||
CurrentBot = health;
|
||||
}
|
||||
}
|
||||
|
||||
void OnBotDied(BotHealth bot)
|
||||
{
|
||||
bot.Died -= OnBotDied;
|
||||
CurrentBot = null;
|
||||
StartCoroutine(RespawnAfterDelay());
|
||||
}
|
||||
|
||||
@@ -55,17 +60,18 @@ public class BotSpawner : MonoBehaviour
|
||||
Spawn();
|
||||
}
|
||||
|
||||
// 在範圍內隨機選點,盡量離玩家遠一點 ; 試幾次都太近就用最後一次的結果
|
||||
// 在範圍內隨機選點 : 要離玩家夠遠,而且不能卡在障礙物或牆裡 ; 試很多次都不行就用最後一次的結果
|
||||
Vector3 PickSpawnPoint()
|
||||
{
|
||||
Vector3 point = transform.position;
|
||||
|
||||
for (int i = 0; i < 20; i++)
|
||||
for (int i = 0; i < 50; 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)
|
||||
bool farEnough = player == null || Vector3.Distance(point, player.position) >= minDistanceFromPlayer;
|
||||
if (farEnough && IsSpaceFree(point))
|
||||
{
|
||||
break;
|
||||
}
|
||||
@@ -74,6 +80,15 @@ public class BotSpawner : MonoBehaviour
|
||||
return point;
|
||||
}
|
||||
|
||||
// 用一個跟機器人差不多大的膠囊檢查這個位置有沒有東西 ; 底部抬高一點,不去碰到地板
|
||||
bool IsSpaceFree(Vector3 feet)
|
||||
{
|
||||
const float radius = 0.4f;
|
||||
Vector3 bottom = feet + Vector3.up * (radius + 0.1f);
|
||||
Vector3 top = feet + Vector3.up * (1.8f - radius);
|
||||
return !Physics.CheckCapsule(bottom, top, radius, ~0, QueryTriggerInteraction.Ignore);
|
||||
}
|
||||
|
||||
// 在 Scene 視窗畫出生成範圍,方便調整
|
||||
void OnDrawGizmosSelected()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
using UnityEngine;
|
||||
|
||||
// 敵人血條 : 裡面那條會依剩餘血量縮短,左邊固定、從右邊往左扣
|
||||
public class EnemyHealthBar : MonoBehaviour
|
||||
{
|
||||
[Header("參照")]
|
||||
public BotSpawner spawner;
|
||||
|
||||
[Tooltip("整個血條 (外框),沒有敵人時隱藏")]
|
||||
public GameObject panel;
|
||||
|
||||
[Tooltip("會縮短的那條 (外框裡面的 Image)")]
|
||||
public RectTransform fill;
|
||||
|
||||
private float fullWidth;
|
||||
private float centerX;
|
||||
private float shownRatio = -1f;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (fill != null)
|
||||
{
|
||||
fullWidth = fill.sizeDelta.x;
|
||||
centerX = fill.anchoredPosition.x;
|
||||
}
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
// 機器人倒下到重生之間先隱藏
|
||||
BotHealth bot = spawner != null ? spawner.CurrentBot : null;
|
||||
if (panel != null) panel.SetActive(bot != null);
|
||||
if (bot == null || fill == null) return;
|
||||
|
||||
float ratio = bot.maxHealth > 0f ? Mathf.Clamp01(bot.Health / bot.maxHealth) : 0f;
|
||||
if (Mathf.Approximately(ratio, shownRatio)) return;
|
||||
shownRatio = ratio;
|
||||
|
||||
// 寬度照比例縮短,再依 Pivot 往左移,讓左邊維持在原位
|
||||
float width = fullWidth * ratio;
|
||||
fill.sizeDelta = new Vector2(width, fill.sizeDelta.y);
|
||||
fill.anchoredPosition = new Vector2(centerX - fill.pivot.x * (fullWidth - width), fill.anchoredPosition.y);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f20c2b1f45472084cbc13187b05e9d8d
|
||||
@@ -0,0 +1,33 @@
|
||||
using UnityEngine;
|
||||
|
||||
// 敵人方位指示 : 箭頭轉向敵人所在的水平方向
|
||||
// 箭頭圖片朝上 = 敵人在正前方 ; 敵人在右邊時箭頭往右轉
|
||||
public class EnemyIndicator : MonoBehaviour
|
||||
{
|
||||
[Header("參照")]
|
||||
public BotSpawner spawner;
|
||||
|
||||
[Tooltip("Main Camera,以它的朝向為基準")]
|
||||
public Camera viewCamera;
|
||||
|
||||
[Tooltip("箭頭 (朝上的圖片)")]
|
||||
public RectTransform pointer;
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (pointer == null) return;
|
||||
|
||||
// 機器人倒下到重生之間先隱藏
|
||||
BotHealth bot = spawner != null ? spawner.CurrentBot : null;
|
||||
bool hasTarget = bot != null && viewCamera != null;
|
||||
pointer.gameObject.SetActive(hasTarget);
|
||||
if (!hasTarget) return;
|
||||
|
||||
// 只看水平方向 : 正數 = 敵人在右邊,負數 = 在左邊
|
||||
Vector3 forward = Vector3.ProjectOnPlane(viewCamera.transform.forward, Vector3.up);
|
||||
Vector3 toBot = Vector3.ProjectOnPlane(bot.transform.position - viewCamera.transform.position, Vector3.up);
|
||||
float angle = Vector3.SignedAngle(forward, toBot, Vector3.up);
|
||||
|
||||
pointer.localEulerAngles = new Vector3(0f, 0f, -angle); // UI 的 Z 軸是逆時針,所以取負號
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 057ccc4b1126d6d4aa881bc4633338b8
|
||||
Reference in new issue
Block a user