👨💻个人主页:@元宇宙-秩沅
👨💻 hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!
👨💻 本文由 秩沅 原创
👨💻 收录于专栏:unityUI专题篇
⭐⭐
文章目录
🎶前言
🅰️
🎶(A)关键逻辑梳理
🎶(B)需求分析
🎶(C)行为实现——血条
😶🌫️:步骤实现
- 1.坐标三个转化
- 2.GUI的原点和屏幕的原点
- 3.结构体的特点回顾——涉及Rect_结构体类型
- 4.血条的显隐
- 5.倒计时的两个方法
- 6.近大远小血条的缩放——待
血条核心代码
//GUI生命函数privatevoidOnGUI(){if(showTime >=0)//血条倒计时展示{//简单的倒计时
showTime -= Time.deltaTime;//世界坐标转屏幕坐标
SreecPosition = Camera.main.WorldToScreenPoint(transform.position);//屏幕坐标转GUI坐标_____GUI的原点在左上角,屏幕坐标原点在左下角
SreecPosition.y = Screen.height - SreecPosition.y;//底图Rect位置参数赋值
GUIPosition1.x = SreecPosition.x -50;
GUIPosition1.y = SreecPosition.y -70;
GUIPosition1.width =100;
GUIPosition1.height =15;//血条图Rect位置参数赋值
GUIPosition2.x = SreecPosition.x -50;
GUIPosition2.y = SreecPosition.y -70;//血条长度和血量同步
GUIPosition2.width =100*(nowBlood/maxBlood);
GUIPosition2.height =15;//绘制底图
GUI.DrawTexture(GUIPosition1, underTexture);//绘制血条
GUI.DrawTexture(GUIPosition2, topTexture);}}
更新敌方坦克
______-
using System.Collections;using System.Collections.Generic;using UnityEngine;//-------------------------------------//—————————————————————————————————————//___________项目: //___________功能:敌方坦克逻辑封装//___________创建者:秩沅_______________//_____________________________________//-------------------------------------publicclassEnemyTank:TankFather{//随机移动点的位置数组public Transform[] randomPosition;//目标位置private Transform target;//目标坦克public Transform Player;//检测范围publicfloat distance =10f;//子弹发射参数public AudioSource shootMusic;//发射音效public GameObject shootball;//发射的子弹类型publicfloat shootSpeed =1000f;//子弹的速度 public Transform[] shootTransform;//发射的组件信息publicbool shootSwitch =false;privatefloat endTime =3f;//子弹发射间隔的时间//血条的参数public Texture underTexture;public Texture topTexture;private Vector3 SreecPosition;private Rect GUIPosition1;private Rect GUIPosition2;publicfloat showTime =0;privatevoidStart(){//属性初始化赋值
maxBlood =500;
nowBlood =500;
attack =30;
HeadSpeed =50;//射击音效关闭
shootMusic.enabled =false;
moveSpeed =10f;//先随机整一个目标点RandomPosition();}privatevoidUpdate(){
transform.LookAt(target);//始终超自己的正方向移动
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);//当距离差不多相等时,再随机目标点if(Vector3.Distance(transform .position ,target .position)<0.5f){RandomPosition();}//范围检测if(Vector3 .Distance(transform .position ,Player.position )<= distance && Player !=null ){//炮口瞄准玩家
Head.transform.LookAt(Player);//倒计时发射子弹
endTime = Mathf.MoveTowards(endTime,0,0.1f);if(endTime <=0){Fire();
endTime =3f;}}}//随机指向一个移动点publicvoidRandomPosition(){if(randomPosition.Length !=0)
target = randomPosition[Random.Range(0, randomPosition.Length)];}//触发检测privatevoidOnTriggerEnter(Collider other){if(other.gameObject.tag =="bullet"){//添加子弹挂载的目标坦克,方便获取该子弹的攻击力,与受伤逻辑相结合
BulletMove ball = other.gameObject.GetComponent<BulletMove>();
TankFather ballTank = ball.Tank.GetComponent<TankFather>();//当子弹不是自己打的到自己身上的时候if(ballTank.tag != gameObject.tag){//扣血Heart(ballTank);}}}publicoverridevoidFire(){//开启射击音效
shootMusic.enabled =true;
shootMusic.Play();for(int i =0; i < shootTransform.Length ; i++){
GameObject ball =Instantiate(shootball, shootTransform[i].position, shootTransform[i].rotation);
BulletMove movScript = ball.GetComponent<BulletMove>();
Rigidbody shootBall = ball.GetComponent<Rigidbody>();
shootBall.AddForce(shootTransform[i].transform.forward * shootSpeed);
movScript.Tank = gameObject;//声明子弹是由谁打出去的}}//死亡行为重写publicoverridevoidDeath(){
base.Death();
GamePlane.SingleInstance.AddScore(10);}//受伤行为重写publicoverridevoidHeart(TankFather other){
base.Heart(other);
showTime =3;//血条倒计时展示}//GUI生命函数privatevoidOnGUI(){if(showTime >=0)//血条倒计时展示{//简单的倒计时
showTime -= Time.deltaTime;//世界坐标转屏幕坐标
SreecPosition = Camera.main.WorldToScreenPoint(transform.position);//屏幕坐标转GUI坐标_____GUI的原点在左上角,屏幕坐标原点在左下角
SreecPosition.y = Screen.height - SreecPosition.y;//底图Rect位置参数赋值
GUIPosition1.x = SreecPosition.x -50;
GUIPosition1.y = SreecPosition.y -70;
GUIPosition1.width =100;
GUIPosition1.height =15;//血条图Rect位置参数赋值
GUIPosition2.x = SreecPosition.x -50;
GUIPosition2.y = SreecPosition.y -70;//血条长度和血量同步
GUIPosition2.width =100*(nowBlood/maxBlood);
GUIPosition2.height =15;//绘制底图
GUI.DrawTexture(GUIPosition1, underTexture);//绘制血条
GUI.DrawTexture(GUIPosition2, topTexture);}}}
更新炮台
using System.Collections;using System.Collections.Generic;using UnityEngine;//-------------------------------------//—————————————————————————————————————//___________项目: //___________功能:敌方坦克逻辑封装//___________创建者:秩沅_______________//_____________________________________//-------------------------------------publicclassEnemyTank:TankFather{//随机移动点的位置数组public Transform[] randomPosition;//目标位置private Transform target;//目标坦克public Transform Player;//检测范围publicfloat distance =10f;//子弹发射参数public AudioSource shootMusic;//发射音效public GameObject shootball;//发射的子弹类型publicfloat shootSpeed =1000f;//子弹的速度 public Transform[] shootTransform;//发射的组件信息publicbool shootSwitch =false;privatefloat endTime =3f;//子弹发射间隔的时间//血条的参数public Texture underTexture;public Texture topTexture;private Vector3 SreecPosition;private Rect GUIPosition1;private Rect GUIPosition2;publicfloat showTime =0;privatevoidStart(){//属性初始化赋值
maxBlood =500;
nowBlood =500;
attack =30;
HeadSpeed =50;//射击音效关闭
shootMusic.enabled =false;
moveSpeed =10f;//先随机整一个目标点RandomPosition();}privatevoidUpdate(){
transform.LookAt(target);//始终超自己的正方向移动
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);//当距离差不多相等时,再随机目标点if(Vector3.Distance(transform .position ,target .position)<0.5f){RandomPosition();}//范围检测if(Vector3 .Distance(transform .position ,Player.position )<= distance && Player !=null ){//炮口瞄准玩家
Head.transform.LookAt(Player);//倒计时发射子弹
endTime = Mathf.MoveTowards(endTime,0,0.1f);if(endTime <=0){Fire();
endTime =3f;}}}//随机指向一个移动点publicvoidRandomPosition(){if(randomPosition.Length !=0)
target = randomPosition[Random.Range(0, randomPosition.Length)];}//触发检测privatevoidOnTriggerEnter(Collider other){if(other.gameObject.tag =="bullet"){//添加子弹挂载的目标坦克,方便获取该子弹的攻击力,与受伤逻辑相结合
BulletMove ball = other.gameObject.GetComponent<BulletMove>();
TankFather ballTank = ball.Tank.GetComponent<TankFather>();//当子弹不是自己打的到自己身上的时候if(ballTank.tag != gameObject.tag){//扣血Heart(ballTank);}}}publicoverridevoidFire(){//开启射击音效
shootMusic.enabled =true;
shootMusic.Play();for(int i =0; i < shootTransform.Length ; i++){
GameObject ball =Instantiate(shootball, shootTransform[i].position, shootTransform[i].rotation);
BulletMove movScript = ball.GetComponent<BulletMove>();
Rigidbody shootBall = ball.GetComponent<Rigidbody>();
shootBall.AddForce(shootTransform[i].transform.forward * shootSpeed);
movScript.Tank = gameObject;//声明子弹是由谁打出去的}}//死亡行为重写publicoverridevoidDeath(){
base.Death();
GamePlane.SingleInstance.AddScore(10);}//受伤行为重写publicoverridevoidHeart(TankFather other){
base.Heart(other);
showTime =3;//血条倒计时展示}//GUI生命函数privatevoidOnGUI(){if(showTime >=0)//血条倒计时展示{//简单的倒计时
showTime -= Time.deltaTime;//世界坐标转屏幕坐标
SreecPosition = Camera.main.WorldToScreenPoint(transform.position);//屏幕坐标转GUI坐标_____GUI的原点在左上角,屏幕坐标原点在左下角
SreecPosition.y = Screen.height - SreecPosition.y;//底图Rect位置参数赋值
GUIPosition1.x = SreecPosition.x -50;
GUIPosition1.y = SreecPosition.y -70;
GUIPosition1.width =100;
GUIPosition1.height =15;//血条图Rect位置参数赋值
GUIPosition2.x = SreecPosition.x -50;
GUIPosition2.y = SreecPosition.y -70;//血条长度和血量同步
GUIPosition2.width =100*(nowBlood/maxBlood);
GUIPosition2.height =15;//绘制底图
GUI.DrawTexture(GUIPosition1, underTexture);//绘制血条
GUI.DrawTexture(GUIPosition2, topTexture);}}}
🎶(D)场景搭建——敌方阵营
🎶(E)场景搭建——通关面板
_____________
😶🌫️:步骤实现:
- 1.面板制作
- 2.数据显示和存储
- 3.循环开始
using System;using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.SceneManagement;//-------------------------------------//—————————————————————————————————————//___________项目: //___________功能:通关面板逻辑相关//___________创建者:秩沅_______________//_____________________________________//-------------------------------------publicclassVictorPlane:BeginFather<VictorPlane>{public Button Require;//确认按钮public InputFram name;//输入的昵称public Label Score;//显示的分数public Label LTime;//显示的时间privateint time;privatevoidStart(){//添加按钮事件
Require.triggerEvent +=()=>{//点击按钮后跳到主场景
SceneManager.LoadScene("BeginScreen");//将数据存储
SaveGameData.DataContorl.SaveRankData(name.ContorlContent.text, Convert.ToInt32(Score.ContorlContent.text), time);};//默认隐藏Hidden();}privatevoidUpdate(){//将光标显示出来
Cursor.visible =true;//分数显示
Score.ContorlContent.text = GamePlane.SingleInstance.laScore.ContorlContent.text;//时间显示
LTime.ContorlContent.text = GamePlane.SingleInstance.laTime.ContorlContent.text;//时间
time = GamePlane.SingleInstance.time;}}
🎶(F)行为实现——通关点触发
using System.Collections;using System.Collections.Generic;using UnityEngine;//-------------------------------------//—————————————————————————————————————//___________项目: //___________功能: 通关触发//___________创建者:秩沅_______________//_____________________________________//-------------------------------------publicclassover:MonoBehaviour{privatevoidOnTriggerEnter(Collider other){if(other.CompareTag("Player")){
Time.timeScale =0;
VictorPlane.SingleInstance.Show();}}}
🎶(G)场景搭建——结束面板
using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.SceneManagement;//-------------------------------------//—————————————————————————————————————//___________项目: ______________//___________功能: 死亡显示面板//___________创建者:秩沅_______________//_____________________________________//-------------------------------------publicclassDiedPlane:BeginFather<DiedPlane>{public Button back;privatevoidStart(){
back.triggerEvent +=()=>{//点击按钮后跳到主场景
SceneManager.LoadScene("BeginScreen");};Hidden();}}
⭐相关文章⭐
⭐【2023unity游戏制作-mango的冒险】-6.关卡设计
⭐【2023unity游戏制作-mango的冒险】-5.攻击系统的简单实现
⭐【2023unity游戏制作-mango的冒险】-4.场景二的镜头和法球特效跟随
⭐【2023unity游戏制作-mango的冒险】-3.基础动作和动画API实现
⭐【2023unity游戏制作-mango的冒险】-2.始画面API制作
⭐【2023unity游戏制作-mango的冒险】-1.场景搭建
⭐“狂飙”游戏制作—游戏分类图鉴(网易游学)
⭐本站最全-unity常用API大全(万字详解),不信你不收藏
你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!
版权归原作者 秩沅 所有, 如有侵权,请联系我们删除。