0


【unity之IMGUI实践】游戏玩法逻辑实现【四】

在这里插入图片描述


👨‍💻个人主页:@元宇宙-秩沅

👨‍💻 hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!

👨‍💻 本文由 秩沅 原创

👨‍💻 收录于专栏:unityUI专题篇
在这里插入图片描述


⭐抽象父类继承实现⭐

在这里插入图片描述


文章目录


🎶前言

在这里插入图片描述


🅰️


🎶(A)常用关键API


在这里插入图片描述
在这里插入图片描述


🎶(B)需求分析


在这里插入图片描述


🎶(C)行为实现——小地图和相机跟随


在这里插入图片描述


  • Target Texture 行为渲染

——————————————在这里插入图片描述
___________________________在这里插入图片描述

  1. using System.Collections;using System.Collections.Generic;using UnityEngine;//-------------------------------------//—————————————————————————————————————//___________项目: //___________功能: 地图相机跟随//___________创建者:_______秩沅_______//_____________________________________//-------------------------------------publicclasscameraFollow:MonoBehaviour{public Transform Tank;private Vector3 offset;publicfloat heiht;privatefloat OffX;privatefloat offZ;privatevoidStart(){
  2. heiht = transform.position.y;}privatevoidLateUpdate()//注意点1:相机相关逻辑存放点{if(Tank == null)return;//注意点2:为空判断//只有相机的Z轴和X轴跟着Tank变化,y则是距离地面的高度
  3. offset.x = Tank.position.x;
  4. offset.z = Tank.position.z;
  5. offset.y = heiht;
  6. gameObject.transform.position = offset;}}

🎶(D)行为实现——捡起武器发射炮弹


在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

😶‍🌫️ 步骤


1.靠近武器,碰撞检测
2.将武器位置于坦克发射位置重合
3.武器中封装了发射方法
4.发射方法中封装了炮弹实例化并且发射的逻辑

坦克基类的封装

  1. using System.Collections;using System.Collections.Generic;using UnityEngine;//-------------------------------------//—————————————————————————————————————//___________项目: //___________功能: 坦克基类——集中子类相同点//___________创建者:______秩沅______//___________________________________//-------------------------------------public abstract classTankFather:MonoBehaviour{//攻击和防御相关publicint attack;publicint defence;publicint nowBlood;publicint maxBlood;//移动和转速相关publicint moveSpeed;publicint RotateSpeed;publicint HeadSpeed;public Transform Head;//击败特效public GameObject diedEffect;//受伤行为publicvirtualvoidHeart(TankFather other){//当攻击力大于防御力时才生效if(other.attack - defence >0){
  2. nowBlood -=(other.attack - defence);}if(nowBlood <=0){
  3. nowBlood =0;Death();}//更新血条
  4. GamePlane.SingleInstance.UpdataBlood(maxBlood ,nowBlood );}//死亡行为publicvirtualvoidDeath(){Destroy(this);//将特效实例化相关逻辑
  5. GameObject effect =Instantiate(diedEffect,this.transform.position ,this.transform.rotation);
  6. AudioSource soudClip =GetComponent<AudioSource>();
  7. soudClip.enabled = MusicContol.SingleMusicContol.nowMusicData.soundSwitch;
  8. soudClip.volume = MusicContol.SingleMusicContol.nowMusicData.soundRoll ;
  9. soudClip.mute =false;
  10. soudClip.playOnAwake =true;}//开火行为public abstract voidFire();//子类中每一个的开火方式都不同,作为抽象方法}

主坦克的运动逻辑的封装

  1. using System.Collections;using System.Collections.Generic;using UnityEngine;//-------------------------------------//—————————————————————————————————————//___________项目: //___________功能: 坦克的移动和旋转//___________创建者:_______秩沅_______//_____________________________________//-------------------------------------publicclassTankSport:TankFather{using System.Collections;using System.Collections.Generic;using UnityEngine;//-------------------------------------//—————————————————————————————————————//___________项目: //___________功能: 坦克的移动和旋转//___________创建者:_______秩沅_______//_____________________________________//-------------------------------------publicclassMainTank:TankFather{//主坦克专有特征public Weapon nowWeapon;private Vector3 zero =newVector3(0,0,0);//特征属性初始化privatevoidAwake(){
  2. Head = transform.GetChild(1).GetChild(0);
  3. maxBlood =100;
  4. nowBlood =100;
  5. attack =30;
  6. defence =10;
  7. moveSpeed =5;
  8. RotateSpeed =50;
  9. HeadSpeed =500;}privatevoidUpdate(){//坦克的移动 = 大小*方向
  10. transform.Translate(Input.GetAxis("Vertical")*Vector3.forward*moveSpeed *Time.deltaTime );//坦克的旋转 = 大小*轴向
  11. transform.Rotate(Input.GetAxis("Horizontal")*Vector3.up *RotateSpeed *Time .deltaTime );//头部炮管的旋转 = 大小*轴向
  12. Head.Rotate(Input.GetAxis("Mouse X")*Vector3.up*HeadSpeed*Time .deltaTime );//左键发射炮弹if(Input.GetMouseButtonDown(0)){Fire();}}//捡武器行为publicvoidChangeWeapon(GameObject weapon){
  13. nowWeapon = weapon.GetComponent<Weapon>();//依附成为子对象______待优化
  14. weapon.gameObject.transform.position = Head.GetChild(0).transform.position ;
  15. weapon.gameObject.transform.rotation = Head.GetChild(0).transform.rotation;
  16. weapon.gameObject.transform.SetParent(Head.GetChild(0));
  17. attack += nowWeapon.attack;}//开火行为重写publicoverridevoidFire(){
  18. nowWeapon.Shoot();}}

武器类的封装

  1. using System.Collections;using System.Collections.Generic;using UnityEngine;//-------------------------------------//—————————————————————————————————————//___________项目: ______________//___________功能: 武器的逻辑相关控制//___________创建者:秩沅_______________//_____________________________________//-------------------------------------publicclassWeapon:MonoBehaviour{public GameObject Tank;//挂载的坦克对象public GameObject bullet;//子弹对象public Transform [] bulletNum;//子弹数量及位置publicint attack;//攻击力 publicfloat bulletSpeed =1000f;//速度private AudioSource shootSound;//发射音效privatevoidStart(){//加载发射音效
  2. shootSound =GetComponent<AudioSource>();//同步设置中音效大小
  3. shootSound.enabled = MusicContol.SingleMusicContol.nowMusicData.soundSwitch;
  4. shootSound.volume = MusicContol.SingleMusicContol.nowMusicData.soundRoll;
  5. shootSound.enabled =false;//加载子弹发射数量
  6. bulletNum =new Transform[transform.GetChild(0).childCount];for(int i =0; i < transform.GetChild(0).childCount ; i++){
  7. bulletNum[i]= transform.GetChild(0).GetChild(i);}}//销毁行为publicvoidVanish(){Destroy(this.gameObject );}//射击行为publicvirtualvoidShoot(){
  8. shootSound.enabled =true;
  9. shootSound.Play();//实例化炮弹,并进行发射for(int i =0; i < bulletNum.Length ; i++){
  10. GameObject ball =Instantiate(bullet, bulletNum[i].position, bulletNum[i].rotation );
  11. BulletMove movScript = ball.GetComponent<BulletMove>();
  12. Rigidbody shootBall = ball.GetComponent<Rigidbody>();
  13. shootBall.AddForce(transform.forward * bulletSpeed);
  14. movScript.Tank = Tank;//声明子弹是由谁打出去的}}//如果碰到标签是主坦克玩家 并且 武器没有主人privatevoidOnCollisionEnter(Collision collision){if(Tank == null && collision.transform.name =="Player"){
  15. Tank = collision.gameObject;
  16. MainTank player = collision.gameObject.GetComponent<MainTank>();
  17. player.ChangeWeapon(this.gameObject);print("玩家");}}}

🎶(E)行为实现——炮弹的销毁和特效


在这里插入图片描述

——————————在这里插入图片描述

  1. using System.Collections;using System.Collections.Generic;using UnityEngine;//-------------------------------------//—————————————————————————————————————//___________项目: ______________//___________功能: 子弹的逻辑相关//___________创建者:秩沅_______________//_____________________________________//-------------------------------------publicclassBulletMove:MonoBehaviour{public GameObject Tank;//挂载在哪个坦克上public GameObject DeidEffect;public AudioSource distorySound;//销毁的音效privatefloat endTime =15f;//销毁倒计时privatevoidStart(){}//子弹的销毁及特效privatevoidOnTriggerEnter(Collider other){if(other.CompareTag("Environment")){if(DeidEffect != null){
  2. GameObject eff =Instantiate(DeidEffect, transform.position, transform.rotation);//音效控制
  3. distorySound = eff.GetComponent<AudioSource>();
  4. distorySound.enabled = MusicContol.SingleMusicContol.nowMusicData.soundSwitch;
  5. distorySound.volume = MusicContol.SingleMusicContol.nowMusicData.soundRoll;
  6. eff.AddComponent<SelfDestroy>();}Destroy(this.gameObject);}}privatevoidUpdate(){//当未碰撞时自动销毁endTimeDistory(gameObject);}//倒计时销毁publicvoidendTimeDistory( GameObject instans){
  7. endTime = Mathf.MoveTowards(endTime,0,0.1f);if(endTime <=0)Destroy(instans);}}

总结:

问题: 为什么炮弹生成了却无法射出 ; 原因: FreezePosition被锁定了

问题:往前走时,发射方向和炮口方向相反

相关文章


⭐【2023unity游戏制作-mango的冒险】-6.关卡设计

⭐【2023unity游戏制作-mango的冒险】-5.攻击系统的简单实现

⭐【2023unity游戏制作-mango的冒险】-4.场景二的镜头和法球特效跟随

⭐【2023unity游戏制作-mango的冒险】-3.基础动作和动画API实现

⭐【2023unity游戏制作-mango的冒险】-2.始画面API制作

⭐【2023unity游戏制作-mango的冒险】-1.场景搭建

⭐“狂飙”游戏制作—游戏分类图鉴(网易游学)

⭐本站最全-unity常用API大全(万字详解),不信你不收藏



你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!


本文转载自: https://blog.csdn.net/m0_64128218/article/details/131759167
版权归原作者 秩沅 所有, 如有侵权,请联系我们删除。

“【unity之IMGUI实践】游戏玩法逻辑实现【四】”的评论:

还没有评论