30天轻松学会unity自制游戏-捡道具

目前的炸弹可以连发,来制作炸弹道具数量,让炸弹有限,直接在Player程序加装炸弹数,开启PlayerControl

	//序列化一个炸弹数3
    [SerializeField] int bomb=3;
	//等等显示还有几个炸弹的数量
    [SerializeField] Text boomNumber = default;

    private void Update()
    {
        horizontalMove = Input.GetAxisRaw("Horizontal");	
		//持续显示炸弹数量
        boomNumber.text = "" + bomb;
        //transform.Translate(Vector3.up * 1 * Time.deltaTime, Space.World);
    }

    public void Boom()
    {
		//如果炸弹数大於0才能发射
        if (bomb > 0)
        {	
			//每次发射减一炸弹数
            bomb -= 1;
			//创造炸弹
            Instantiate(boom, transform.position, Quaternion.identity);
        }
    }
	//碰撞触发
    private void OnTriggerEnter2D(Collider2D collision)
    {

        if (collision.tag == "EnemyBullet")
        {
            hp -= 5f;
            hpBar.fillAmount = hp / hpMax;

            if (hp <= 0)
            {
                anim.SetBool("die", true);
                dieWindow.SetActive(true);
                Destroy(gameObject, 0.5f);
            }
        }
		//如果碰到标签叫"boom"的执行
        if(collision.tag =="boom")
        {
			//炸弹数加一
            bomb += 1;
			//删除碰撞到的物件
            Destroy(collision.gameObject);
        }
    }

制作炸弹数量的文字框
https://ithelp.ithome.com.tw/upload/images/20210923/20140598gc0QjghuxA.png
把炸弹数量的文字框装在Player的BoomNumber上面,这样等等才会显示炸弹数量
https://ithelp.ithome.com.tw/upload/images/20210923/20140598fAHAfGGZII.png
把素材库里的道具炸弹拿出来,放上collider记得勾isTrigger後制作成预置物
https://ithelp.ithome.com.tw/upload/images/20210923/20140598o33k53rHHg.png
记得把炸弹的标签改成boom
https://ithelp.ithome.com.tw/upload/images/20210923/20140598aA1iVjq68c.png
再来就是让敌人死後喷道具炸弹让玩家捡,开启敌机程序EnemyAI,简单写几行就好

	//序列化一个物件(炸弹道具)(等等在unity要赋予此物件)
    [SerializeField] GameObject boom = null;
    private void OnTriggerEnter2D(Collider2D collision)
    {
        
        if(collision.tag == "PlayerBullet")
        {
            hp -= 5f;
            anim.SetTrigger("hit");
            if (hp <= 0)
            {
				//死掉时必定产生炸弹道具
               Instantiate(boom, transform.position, Quaternion.identity);
                Destroy(gameObject);
            }
        }
    }

最後把炸弹道具的预置物,放到敌机身上即可
https://ithelp.ithome.com.tw/upload/images/20210923/20140598gnlRW8ZkjX.png
应该运用同原理可以制作吃金币,运用Boss的机率写法可以让掉落机率不一样,这边就不一一介绍,有兴趣的可以自行尝试看看,现在应该是可以稍微制作出一些属於自己的游戏内容罗~


<<:  Component 鬼牌(一): 看 props 决定 Component

>>:  Node.js介绍

如何建立 Windows USB 安装随身碟-适用 Win 10, Win Server 2019

使用 USB 跟传统 DVD 一样可以正常安装 Windows,而且携带方便,还不需要准备光碟机。 ...

[day-16] 认识Python的资料结构!(Part .3)

用List实现多层容器   何谓 多层容器 ?我们前面说过List可以存放 『任意型别』 ,其中也包...

Day 3 Capsule Network

Day 3 Capsule Network 前言 昨天讲到CNN的限制,那今天就要开始介绍甚麽是胶囊...

Day03:Set User Name(存放使用者名称)

全文同步於个人 Docusaurus Blog 在这一章中,主要处理下述两个问题: 初始进入页面的建...

[第九只羊] 迷雾森林舞会II 房间座位设定

天亮了 昨晚是平安夜 关於迷雾森林故事 粉红烟花三个月 由於黑洞把12只 animal 吸走後的烟 ...