Unity-3D捕鱼达人小游戏开发 —— 炮台子弹发射

为子弹创建出生地
这里写图片描述

在点击按钮的时候一样会射出子弹,使用

EventSystem.current.IsPointerOverGameObject() == false;

来解决,但是现在一个子弹都射不出来,需要将背景的属性修改,是否是射线检测的目标
这里写图片描述
给子弹添加属性脚本文件,添加碰撞体
BulletAttribute.cs

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

public class BulletAttribute : MonoBehaviour {
    // 子弹速度
    public float speed;
    // 子弹伤害
    public int damage;
    // 子弹爆炸后网的形状
    public GameObject webPrefab;

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Border"){
            Destroy(gameObject);
        }
        if (collision.tag == "Fish"){
            // 碰到鱼的时候就实例化网
            GameObject web = Instantiate(webPrefab);

            web.transform.SetParent(gameObject.transform.parent, false);
            web.transform.position = gameObject.transform.position;

            // 获取到网的属性,给网附加伤害
            web.GetComponent<WebAttribute>().damage = damage;

            // 销毁鱼
            Destroy(gameObject);
        }
    }
}

这里写图片描述
这里写图片描述
GameController.cs关于开火的方法

void Fire(){
        // 当前使用的子弹
        GameObject[] usingBullets = bulletObjects1;
        // 发射子弹中的哪一种
        int bulletIndex;
        // 如果按下鼠标左键,并且判断是否和UI相撞,解决UI穿透
        if (Input.GetMouseButtonDown(0) && EventSystem.current.IsPointerOverGameObject() == false){
            // 加四次钱换一次炮台
            switch(costIndex / 4)
            {
                case 0:
                    usingBullets = bulletObjects1;
                    break;
                case 1:
                    usingBullets = bulletObjects2;
                    break;
                case 2:
                    usingBullets = bulletObjects3;
                    break;
                case 3:
                    usingBullets = bulletObjects4;
                    break;
                case 4:
                    usingBullets = bulletObjects5;
                    break;
            }
            // 如果等级大于20级,那资源就不够,数组就会越界,
            bulletIndex = (lv % 10 >= 9) ? 9 : lv % 10;

            // 实例化
            GameObject bullet = Instantiate (usingBullets [bulletIndex]);
            // 设置父体,false:不保留世界坐标
            bullet.transform.SetParent (bulletsHolder, false);
            // 调节bullet的localPosition和localRotation
            bullet.transform.position = 
                gunObjects [costIndex / 4].transform.Find ("FirePosition").transform.position;
            bullet.transform.rotation = 
                gunObjects [costIndex / 4].transform.Find ("FirePosition").transform.rotation;

            // 给子弹添加伤害
            bullet.GetComponent<BulletAttribute>().damage = shootCosts[costIndex];


            // 给子弹加上直线移动脚本
            // 调整子弹方向
            bullet.AddComponent<Ef_AutoMove>().dir = Vector3.up;
            // 调整子弹速度
            bullet.GetComponent<Ef_AutoMove>().speed = bullet.GetComponent<BulletAttribute>().speed;
}

子弹爆炸后会有渔网,需要给渔网的预制件添加属性脚本文件,还要添加碰撞体
WebAttribute.cs

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

public class WebAttribute : MonoBehaviour
{
    // 消失时间
    public float disapperTime;
    // 网的伤害
    public int damage;

    private void Start()
    {
        // 时间一到就销毁自己
        Destroy(gameObject, disapperTime);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Fish")
        {
            // 给鱼发消息让鱼受伤,传入伤害值
            collision.SendMessage("ReceiveDamage", dama
                                  ge);
        }
    }

}
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值