👨💻个人主页:@元宇宙-秩沅
👨💻 hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!
👨💻本文由 秩沅 原创
👨💻 收录于专栏:unityUI专题篇
⭐抽象父类继承实现⭐
文章目录
⭐抽象父类继承实现⭐🎶前言🎶(==A==)常用关键API🎶(==B==)需求分析🎶(==C==)行为实现——小地图和相机跟随🎶(==D==)行为实现——捡起武器发射炮弹🎶(==E==)行为实现——炮弹的销毁和特效总结: 问题:往前走时,发射方向和炮口方向相反🎶前言
🅰️
🎶(A)常用关键API
🎶(B)需求分析
🎶(C)行为实现——小地图和相机跟随
Target Texture 行为渲染——————————————
___________________________
using System.Collections;using System.Collections.Generic;using UnityEngine;//-------------------------------------//—————————————————————————————————————//___________项目://___________功能:地图相机跟随//___________创建者:_______秩沅_______//_____________________________________//-------------------------------------public class cameraFollow : MonoBehaviour{public Transform Tank;private Vector3 offset;public float heiht;private float OffX;private float offZ;private void Start(){heiht = transform.position.y; }private void LateUpdate() //注意点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 class TankFather : MonoBehaviour{//攻击和防御相关public int attack;public int defence; public int nowBlood;public int maxBlood;//移动和转速相关public int moveSpeed;public int RotateSpeed;public int HeadSpeed;public Transform Head;//击败特效public GameObject diedEffect;//受伤行为public virtual void Heart(TankFather other){//当攻击力大于防御力时才生效if (other.attack - defence > 0){nowBlood -= (other.attack - defence);}if(nowBlood <= 0 ){nowBlood = 0;Death();}//更新血条GamePlane.SingleInstance.UpdataBlood(maxBlood ,nowBlood );}//死亡行为public virtual void Death(){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 void Fire(); //子类中每一个的开火方式都不同,作为抽象方法}
主坦克的运动逻辑的封装
using System.Collections;using System.Collections.Generic;using UnityEngine;//-------------------------------------//—————————————————————————————————————//___________项目: //___________功能: 坦克的移动和旋转//___________创建者:_______秩沅_______//_____________________________________//-------------------------------------public class TankSport : TankFather{using System.Collections;using System.Collections.Generic;using UnityEngine;//-------------------------------------//—————————————————————————————————————//___________项目: //___________功能: 坦克的移动和旋转//___________创建者:_______秩沅_______//_____________________________________//-------------------------------------public class MainTank : TankFather{//主坦克专有特征public Weapon nowWeapon;private Vector3 zero = new Vector3(0,0,0);//特征属性初始化private void Awake(){Head = transform.GetChild(1).GetChild(0);maxBlood = 100;nowBlood = 100;attack = 30;defence = 10;moveSpeed = 5;RotateSpeed = 50;HeadSpeed = 500;}private void Update(){//坦克的移动 = 大小*方向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();}}//捡武器行为public void ChangeWeapon(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;}//开火行为重写public override void Fire() {nowWeapon.Shoot(); }}
武器类的封装
using System.Collections;using System.Collections.Generic;using UnityEngine;//-------------------------------------//—————————————————————————————————————//___________项目: ______________//___________功能: 武器的逻辑相关控制//___________创建者:秩沅_______________//_____________________________________//-------------------------------------public class Weapon :MonoBehaviour {public GameObject Tank; //挂载的坦克对象public GameObject bullet; //子弹对象public Transform [] bulletNum;//子弹数量及位置public int attack; //攻击力 public float bulletSpeed = 1000f; //速度private AudioSource shootSound; //发射音效private void Start(){//加载发射音效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);}}//销毁行为public void Vanish(){Destroy(this.gameObject );}//射击行为public virtual void Shoot(){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; //声明子弹是由谁打出去的}}//如果碰到标签是主坦克玩家 并且 武器没有主人private void OnCollisionEnter(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;//-------------------------------------//—————————————————————————————————————//___________项目: ______________//___________功能: 子弹的逻辑相关//___________创建者:秩沅_______________//_____________________________________//-------------------------------------public class BulletMove : MonoBehaviour{public GameObject Tank;//挂载在哪个坦克上public GameObject DeidEffect;public AudioSource distorySound; //销毁的音效private float endTime = 15f; //销毁倒计时private void Start(){}//子弹的销毁及特效private void OnTriggerEnter(Collider other){if (pareTag("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);} }private void Update(){//当未碰撞时自动销毁endTimeDistory(gameObject);}//倒计时销毁public void endTimeDistory( GameObject instans){endTime = Mathf.MoveTowards(endTime, 0, 0.1f);if (endTime <= 0) Destroy(instans);}}
总结:
问题: 为什么炮弹生成了却无法射出 ; 原因: FreezePosition被锁定了
问题:往前走时,发射方向和炮口方向相反
⭐相关文章⭐
⭐【unity游戏制作-mango的冒险】-6.关卡设计
⭐【unity游戏制作-mango的冒险】-5.攻击系统的简单实现
⭐【unity游戏制作-mango的冒险】-4.场景二的镜头和法球特效跟随
⭐【unity游戏制作-mango的冒险】-3.基础动作和动画API实现
⭐【unity游戏制作-mango的冒险】-2.始画面API制作
⭐【unity游戏制作-mango的冒险】-1.场景搭建
⭐“狂飙”游戏制作—游戏分类图鉴(网易游学)
⭐本站最全-unity常用API大全(万字详解),不信你不收藏
你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!