为了账号安全,请及时绑定邮箱和手机立即绑定

敌人死亡后物品掉落

敌人死亡后物品掉落

C#
慕后森 2022-06-12 16:20:40
这是几乎可以完全运行的修改后的代码。我已经评论了最后几个错误所在的位置。基本上,每帧都会调用 randNum,因为此代码位于更新函数内部。当敌人死亡时,HP 值为 0,我可能需要将敌人的HP 重新加载到任何超过 0 的值。但是,这是最好的方法吗?因为它可能会改变我的其他敌人的HP?第二个问题是当敌人死亡时,所有其他这种类型的敌人都会生成一个物品。如何将其修复到仅在已死亡的敌人身上生成的位置?我在想,因为我的 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 positionprivate 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);        }        }    }  }
查看完整描述

1 回答

?
当年话下

TA贡献1890条经验 获得超9个赞

因此,为了使代码正常工作,我必须从 Update() 内部删除代码。然后我创建了一个函数并将我的代码粘贴到它的 DropItem() 中。然后我不得不从代码中完全删除enemyHP <=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);


        }

   }


查看完整回答
反对 回复 2022-06-12
  • 1 回答
  • 0 关注
  • 258 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号