敌人死亡后掉落物品

时间:2018-12-13 23:52:06

标签: c# unity3d

这是经过修改的代码,几乎可以正常运行。我已经评论了最后几个错误的位置。基本上,每帧都会调用randNum,因为此代码位于更新函数内部。当敌人死亡时,HP值为0,我可能需要重新加载敌方HP到大于0的任何值。但这是最好的方法吗?因为这可能会改变我的其他敌人?

第二个问题是当敌人死亡时,所有其他此类敌人会生成一个物品。我该如何解决它只在死去的敌人身上产卵的地方?我在想,因为我的Epos变量已经抓住了所有敌人的问题。我认为我需要更好的方法?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class ItemDrop : MonoBehaviour
{
  [SerializeField]
  private GameObject[] itemList; // Stores the game items
  private int itemNum; // Selects a number to choose from the itemList
  private int randNum; // chooses a random number to see if loot os dropped- Loot chance
  private Transform Epos; // enemy position


private void Start()
{

    Epos = GetComponent<Transform>();
    Debug.Log(itemList);
}
private void Update()
{
    if(EnemyDMG.enemyHP <= 0)
    {



        // This number get's called non-stop after enemy dies
        // This causes non-stop item drops
        // I need to know the logic to make this code happen only once per kill
            randNum = Random.Range(0, 101); // 100% total for determining loot chance;
            Debug.Log("Random Number is " + randNum);

        // The code below runs now and instantiates objects
        // However, all of the similar enemies that are alive drop items
         // at their location. I need to know how to make an item drop
        // only at the lace the enemy was killed
        if(randNum >= 95) // Star Tablet drop itemList[2] currently
        {

            itemNum = 2;// grabs the star tab
            Instantiate(itemList[itemNum], Epos.position, Quaternion.identity);


        }
        else if(randNum > 75 && randNum < 95) // Extra life drop itemList[1] currently
        {

            itemNum = 1;// grabs the star tab
            Instantiate(itemList[itemNum], Epos.position, Quaternion.identity);

        }
        else if(randNum > 40 && randNum <= 75)// Health Heart drop itemList[0] currently
        {

            itemNum = 0;// grabs the star tab
            Instantiate(itemList[itemNum], Epos.position, Quaternion.identity);

        }
    }
  }


 }

1 个答案:

答案 0 :(得分:0)

因此,要使代码正常工作,我必须从Update()内部删除代码。然后,我创建了一个函数,并将代码粘贴到了DropItem()中。然后,我不得不从代码中完全删除敌方HP <= 0。

下一步是进入我的EnemyDamage脚本,创建一个新变量private ItemDrop getItem,最后,在OnCollisionEnter2D()的内部,我添加了一个在敌人死亡时快速检查ItemDrop的方法。下面的源代码。

最终完成的工作代码是:

   using System.Collections;
   using System.Collections.Generic;
   using UnityEngine;


 public class ItemDrop : MonoBehaviour
 {
 [SerializeField]
private GameObject[] itemList; // Stores the game items
private int itemNum; // Selects a number to choose from the itemList
private int randNum; // chooses a random number to see if loot os dropped- Loot chance
private Transform Epos; // enemy position


private void Start()
{
   // itemList = CamFollow.itemListPass;
    Epos = GetComponent<Transform>();
    Debug.Log(itemList);
}

public void DropItem()
{



        randNum = Random.Range(0, 101); // 100% total for determining loot chance;
        Debug.Log("Random Number is " + randNum);


        if (randNum >= 95) // Star Tablet drop itemList[2] currently
        {
            itemNum = 2;// grabs the star tab
            Instantiate(itemList[itemNum], Epos.position, Quaternion.identity);


        }
        else if (randNum > 75 && randNum < 95) // Extra life drop itemList[1] currently
        {

            itemNum = 1;// grabs the star tab
            Instantiate(itemList[itemNum], Epos.position, Quaternion.identity);

        }
        else if (randNum > 40 && randNum <= 75)// Health Heart drop itemList[0] currently
        {

            itemNum = 0;// grabs the star tab
            Instantiate(itemList[itemNum], Epos.position, Quaternion.identity);

        }


}// End of drop item

}

****************************************现在在我的EnemyDMG脚本中:(以下)*** **************************************

 public class EnemyDMG : MonoBehaviour
{
  private int enemyHP;
  private ItemDrop getItem;

private void Start()
{
    enemyHP = Random.Range(50, 200);
    getItem = GetComponent<ItemDrop>();


       }

      // Below this code is inside of a simple OnCollisionEnter2D() function
     // The enemy damage is calculated inside of this same function
     // so I decided not to add any source code not directly relevant to my question
        if(enemyHP <= 0)
        {
            if (getItem != null)
            {
                getItem.DropItem();
                Debug.Log("Dropped an Item " + getItem);
            }
            Destroy(gameObject);

        }
   }