👨💻个人主页:@元宇宙-秩沅
👨💻 hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!
👨💻 本文由 秩沅 原创
👨💻 收录于专栏:unityUI专题篇
⭐抽象父类继承实现⭐
文章目录
🎶前言
🅰️
🎶(A)常用关键API
🎶(B)需求分析
🎶(C)行为实现——小地图和相机跟随
- Target Texture 行为渲染
——————————————
___________________________
using System.Collections;using System.Collections.Generic;using UnityEngine;//-------------------------------------//—————————————————————————————————————//___________项目: //___________功能: 地图相机跟随//___________创建者:_______秩沅_______//_____________________________________//-------------------------------------publicclasscameraFollow:MonoBehaviour{public Transform Tank;private Vector3 offset;publicfloat heiht;privatefloat OffX;privatefloat offZ;privatevoidStart(){
heiht = transform.position.y;}privatevoidLateUpdate()//注意点1:相机相关逻辑存放点{if(Tank == null)return;//注意点2:为空判断//只有相机的Z轴和X轴跟着Tank变化,y则是距离地面的高度
offset.x = Tank.position.x;
offset.z = Tank.position.z;
offset.y = heiht;
gameObject.transform.position = offset;}}
🎶(D)行为实现——捡起武器发射炮弹
😶🌫️ 步骤:
1.靠近武器,碰撞检测
2.将武器位置于坦克发射位置重合
3.武器中封装了发射方法
4.发射方法中封装了炮弹实例化并且发射的逻辑
坦克基类的封装
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){
nowBlood -=(other.attack - defence);}if(nowBlood <=0){
nowBlood =0;Death();}//更新血条
GamePlane.SingleInstance.UpdataBlood(maxBlood ,nowBlood );}//死亡行为publicvirtualvoidDeath(){Destroy(this);//将特效实例化相关逻辑
GameObject effect =Instantiate(diedEffect,this.transform.position ,this.transform.rotation);
AudioSource soudClip =GetComponent<AudioSource>();
soudClip.enabled = MusicContol.SingleMusicContol.nowMusicData.soundSwitch;
soudClip.volume = MusicContol.SingleMusicContol.nowMusicData.soundRoll ;
soudClip.mute =false;
soudClip.playOnAwake =true;}//开火行为public abstract voidFire();//子类中每一个的开火方式都不同,作为抽象方法}
主坦克的运动逻辑的封装
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(){
Head = transform.GetChild(1).GetChild(0);
maxBlood =100;
nowBlood =100;
attack =30;
defence =10;
moveSpeed =5;
RotateSpeed =50;
HeadSpeed =500;}privatevoidUpdate(){//坦克的移动 = 大小*方向
transform.Translate(Input.GetAxis("Vertical")*Vector3.forward*moveSpeed *Time.deltaTime );//坦克的旋转 = 大小*轴向
transform.Rotate(Input.GetAxis("Horizontal")*Vector3.up *RotateSpeed *Time .deltaTime );//头部炮管的旋转 = 大小*轴向
Head.Rotate(Input.GetAxis("Mouse X")*Vector3.up*HeadSpeed*Time .deltaTime );//左键发射炮弹if(Input.GetMouseButtonDown(0)){Fire();}}//捡武器行为publicvoidChangeWeapon(GameObject weapon){
nowWeapon = weapon.GetComponent<Weapon>();//依附成为子对象______待优化
weapon.gameObject.transform.position = Head.GetChild(0).transform.position ;
weapon.gameObject.transform.rotation = Head.GetChild(0).transform.rotation;
weapon.gameObject.transform.SetParent(Head.GetChild(0));
attack += nowWeapon.attack;}//开火行为重写publicoverridevoidFire(){
nowWeapon.Shoot();}}
武器类的封装
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(){//加载发射音效
shootSound =GetComponent<AudioSource>();//同步设置中音效大小
shootSound.enabled = MusicContol.SingleMusicContol.nowMusicData.soundSwitch;
shootSound.volume = MusicContol.SingleMusicContol.nowMusicData.soundRoll;
shootSound.enabled =false;//加载子弹发射数量
bulletNum =new Transform[transform.GetChild(0).childCount];for(int i =0; i < transform.GetChild(0).childCount ; i++){
bulletNum[i]= transform.GetChild(0).GetChild(i);}}//销毁行为publicvoidVanish(){Destroy(this.gameObject );}//射击行为publicvirtualvoidShoot(){
shootSound.enabled =true;
shootSound.Play();//实例化炮弹,并进行发射for(int i =0; i < bulletNum.Length ; i++){
GameObject ball =Instantiate(bullet, bulletNum[i].position, bulletNum[i].rotation );
BulletMove movScript = ball.GetComponent<BulletMove>();
Rigidbody shootBall = ball.GetComponent<Rigidbody>();
shootBall.AddForce(transform.forward * bulletSpeed);
movScript.Tank = Tank;//声明子弹是由谁打出去的}}//如果碰到标签是主坦克玩家 并且 武器没有主人privatevoidOnCollisionEnter(Collision collision){if(Tank == null && collision.transform.name =="Player"){
Tank = collision.gameObject;
MainTank player = collision.gameObject.GetComponent<MainTank>();
player.ChangeWeapon(this.gameObject);print("玩家");}}}
🎶(E)行为实现——炮弹的销毁和特效
——————————
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){
GameObject eff =Instantiate(DeidEffect, transform.position, transform.rotation);//音效控制
distorySound = eff.GetComponent<AudioSource>();
distorySound.enabled = MusicContol.SingleMusicContol.nowMusicData.soundSwitch;
distorySound.volume = MusicContol.SingleMusicContol.nowMusicData.soundRoll;
eff.AddComponent<SelfDestroy>();}Destroy(this.gameObject);}}privatevoidUpdate(){//当未碰撞时自动销毁endTimeDistory(gameObject);}//倒计时销毁publicvoidendTimeDistory( GameObject instans){
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大全(万字详解),不信你不收藏
你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!
版权归原作者 秩沅 所有, 如有侵权,请联系我们删除。