【发布时间】:2015-02-24 20:44:49
【问题描述】:
我正在制作 FPS 游戏。它具有增加玩家健康的可拾取能力(果汁/能量)。 现在我可以用一个立方体来做到这一点(只需使用一个立方体进行测试)。 OnTriggerEnter 就像一个魅力,它增加了玩家的健康并保持健康栏。 但是,当我实例化它时,生成的立方体的触发器不起作用。
我在下面添加了“Spawner”和“HealthIncrease”脚本。哦,是的,我已经添加了刚体/盒子碰撞器。
这是我的 Spawner 脚本(我将它添加到 Empty Game Object)
public class Spawner: MonoBehaviour {
public GameObject Power;
public GameObject player;
public float spawnOffset=3.0f;
public float spawnDelay = 5;
int currentPowerUpCount;
int currentWaveNumber =1;
Vector3 randomSpawnPoint{
get{
int randIndex = UnityEngine.Random.Range(0, transform.childCount-1);
var position = transform.GetChild(randIndex).position + UnityEngine.Random.insideUnitSphere * spawnOffset;
position.y =0;
return position;
}
}
void Start(){
currentPowerUpCount = currentWaveNumber * 3;
Spawn ();
}
void Update(){
CheckifReadySpawn ();
}
void Spawn ()
{
Debug.Log ("spawm" + currentWaveNumber);
for (int i = 0; i < 10; i++) {
var enemyGameobject = (GameObject)Instantiate (Power, randomSpawnPoint, Quaternion.identity);
}
}
void CheckifReadySpawn ()
{
if (currentPowerUpCount <= 0) {
currentWaveNumber++;
currentPowerUpCount = currentWaveNumber * 5;
Invoke ("Spawn", spawnDelay);
}
}
}
这是 HealthIncrease(我将其添加到立方体中)
public class HealthIncrease : MonoBehaviour {
public UISlider healthBar;
public GameObject player;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void OnTriggerEnter (Collider collider) {
if (collider.CompareTag("Player"))
PickItUp ();
}
void PickItUp ()
{
var playerStats = (Stats)player.GetComponent<Stats> ();
if (playerStats.health >= 500) {
return;
}
else {
playerStats.health += 50;
Destroy (this.gameObject);
healthBar.value = playerStats.health / 500;
}
}
}
【问题讨论】:
-
如果您有一个可以工作的对象,请尝试将该对象分配给
public GameObject Power;这样您就知道是脚本还是对象不正确。 -
是的,我就是这么做的。游戏对象有效,但脚本无效。无论如何,我只是放手并添加了其他东西而不是可拾取物
标签: c# unity3d instantiation