简体   繁体   English

生成的对象未触发Unity3D

[英]Spawned objects not triggering Unity3D

I am making an FPS game. 我正在制作FPS游戏。 It has pickup-ables (juices/energy) which increases the player's health. 它具有可拾取的物品(果汁/能量),可以提高玩家的健康状况。 Now I'm able to do that with a single cube (just using a cube for testing). 现在,我可以使用单个多维数据集做到这一点(只需使用多维数据集进行测试)。 The OnTriggerEnter works like a charm, it increases the players health as well as maintains the health bar. OnTriggerEnter就像一个护身符,可以增加玩家的生命值并保持生命值。 But, when I instantiate it, the spawned cubes' triggers don't work. 但是,当我实例化它时,生成的多维数据集触发器不起作用。

I've added my "Spawner" and "HealthIncrease" scripts below. 我在下面添加了“ Spawner”和“ HealthIncrease”脚本。 Oh yeah, and I have added rigid bodies/box colliders everything. 哦,是的,我添加了刚体/撞机盒的所有内容。

This is my Spawner Script(I add this to the Empty Game Object) 这是我的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);
        }
    }



}

This is the HealthIncrease (I add this to the cube) 这是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;
        }
    }


}

I'm guessing the prefab for the pickup does not have a collider on it. 我猜皮卡的预制件上没有对撞机。 Note: for collision to work at least one of the objects needs to have a rigidbody and both need to have colliders. 注意:要使碰撞起作用,至少一个对象需要具有刚体,并且两个对象都必须具有对撞机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM