gpt4 book ai didi

javascript - 统一/如何设置敌人生成的限制?

转载 作者:行者123 更新时间:2023-11-30 09:40:28 25 4
gpt4 key购买 nike

我设法生成了敌人,但它们一直在生成。如何设置限制,避免不断生成?

我已经尝试添加 spawnLimit 和 spawnCounter 但无法让它工作。

var playerHealth = 100;     // Reference to the player's heatlh.
var enemy : GameObject; // The enemy prefab to be spawned.
var spawnTime : float = 3f; // How long between each spawn.
var spawnPoints : Transform[]; // An array of the spawn points this enemy can spawn from.


function Start ()
{
// Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
InvokeRepeating ("Spawn", spawnTime, spawnTime);
}


function Spawn ()
{
// If the player has no health left...
if(playerHealth <= 0f)
{
// ... exit the function.
return;
}


// Find a random index between zero and one less than the number of spawn points.
var spawnPointIndex : int = Random.Range (0, spawnPoints.Length);

// Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
}

最佳答案

你可以使用这种计数器:

int maxEnemies = 100;
int enemiyCounter = 0;

并在 Spawn() 中添加:

if(enemiyCounter < maxEnemies){
Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
enemyCounter++;
}

代替

Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);

关于javascript - 统一/如何设置敌人生成的限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41368157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com