Unity坦克大战开发全流程——游戏场景——主玩家——武器和子弹对象

游戏场景------主玩家------武器和子弹对象

武器

类字段

开火方法

然后再玩家类中关联武器

再调用武器的开火方法

记录武器的拥有者

设置武器的拥有者

为什么要记录和设置武器的拥有者?

因为我们要对子弹的拥有者进行设置,所以就要通过武器的拥有者来间接设置子弹的拥有者。

子弹

类字段

让子弹向前移动

自己再新建一个子弹预制体用于存储子弹

然后为子弹添加一个碰撞器和刚体组件,并将其碰撞器设置为is Tigger

子弹的碰撞逻辑

先为墙壁和地面添加一个标签

设置子弹的拥有者

将武器的拥有者设置为子弹的拥有者(它们都是TankBaseObj类型的变量)这样就可以来进行伤害判断了。

当子弹碰到物体时播放特效

跟子弹一样,新建一个特效预制体

然后再将特效预制体拖拽给子弹预制体即可

为特效预制体添加一个audioSoucre,并为其添加一个音效

控制音效

直到当前WeaponObj和BulletObj的代码如下

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WeaponObj : MonoBehaviour
{
    //该武器要实例化的子弹对象
    public GameObject bullet;
    //关联的炮口位置
    public Transform[] shootPos;
    //记录武器的拥有者
    public TankBaseObj fatherObj;
    


    //开火方法
    public void Fire()
    {
        //有多少个炮口就创建多少个子弹
        for (int i = 0; i < shootPos.Length; i++)
        {
            GameObject obj = Instantiate(bullet, shootPos[i].transform.position, shootPos[i].transform.rotation);
            //控制子弹做什么
            BulletObj bulletObj = obj.GetComponent<BulletObj>();
            bulletObj.SetFather(fatherObj);
        }
    }

    //设置武器的拥有者
    public void SetFather(TankBaseObj obj)
    {
        fatherObj = obj;
    }
}
cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BulletObj : MonoBehaviour
{
    //子弹的移动速度
    public int moveSpeed = 50;
    //是谁发射的子弹
    public TankBaseObj fatherObj;
    //关联的特效
    public GameObject effObj;

    void Update()
    {
        transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
    }

    //当子弹发生碰撞时
    private void OnTriggerEnter(Collider other)
    {
        //子弹销毁的三种情况:1.碰到墙壁或地面 2.敌人射到了玩家 3.玩家射到了敌人
        if (other.CompareTag("Cube")||other.CompareTag("Player")&&fatherObj.CompareTag("Monster")|| other.CompareTag("Monster") && fatherObj.CompareTag("Player"))
        {
            //执行伤害逻辑
            TankBaseObj obj = other.GetComponent<TankBaseObj>();
            if (obj != null)
            {
                obj.Wound(fatherObj);
            }

            if (effObj != null)
            {

                GameObject eff = Instantiate(effObj, transform.position, transform.rotation);
                //获取特效预制体上挂载的音源组件并控制其音效
                AudioSource audioSource = eff.GetComponent<AudioSource>();
                audioSource.volume = GameDataMgr.Instance.musicData.soundValue;
                audioSource.mute = !GameDataMgr.Instance.musicData.isOpenSound;
                if (other.CompareTag("Player"))
                    audioSource.Play();
            }

            Destroy(gameObject);
        }
    }

    //设置子弹的拥有者
    public void SetFather(TankBaseObj obj)
    {
        fatherObj = obj;
    }
}
相关推荐
qq_428639615 小时前
虚幻基础:安装插件
游戏引擎·虚幻
qq 180809518 小时前
从零构建一个多目标多传感器融合跟踪器
unity
平行云8 小时前
实时云渲染支持在网页上运行UE5开发的3A大作Lyra项目
unity·云原生·ue5·webgl·虚拟现实·实时云渲染·像素流送
AA陈超8 小时前
虚幻引擎5 GAS开发俯视角RPG游戏 P07-18.生成火球术
c++·游戏·ue5·游戏引擎·虚幻
鹏飞于天8 小时前
Shader compiler initialization error: Failed to read D3DCompiler DLL file
unity
_大学牲10 小时前
Flutter 勇闯2D像素游戏之路(三):人物与地图元素的交互
flutter·游戏·游戏开发
wonder1357910 小时前
UGUI重建流程和优化
unity·游戏开发·ugui
游戏技术分享11 小时前
【鸿蒙游戏技术分享 第71期】资质证明文件是否通过
游戏·华为·harmonyos
reddingtons11 小时前
PS 参考图像:线稿上色太慢?AI 3秒“喂”出精细厚涂
前端·人工智能·游戏·ui·aigc·游戏策划·游戏美术
Doc.S13 小时前
多无人机任务自定义(基于ZJU-FAST-Lab / EGO-Planner-v2)
游戏引擎·无人机·cocos2d