放置障礙物,優化玩家尋找敵方方式
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()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user