[Unity学习教程] 【unity之IMGUI实践】游戏竣事流程封装实现【七】

[复制链接]
查看669 | 回复0 | 2023-8-16 16:30:28 | 显示全部楼层 |阅读模式 来自 中国北京


‍个人主页:@元宇宙-秩沅
‍ hallo 欢迎 点赞 收藏⭐ 留言 加关注✅!
本文由 秩沅 原创
‍ 收录于专栏unityUI专题篇






  

媒介





(A)关键逻辑梳理




(B)需求分析





(C)行为实现——血条




‍️:步调实现


  • 1.坐标三个转化
  • 2.GUI的原点和屏幕的原点
  • 3.结构体的特点回顾——涉及Rect_结构体范例
  • 4.血条的显隐
  • 5.倒计时的两个方法
  • 6.近大远小血条的缩放——待


   血条核心代码
  1. //GUI生命函数
  2.     private void OnGUI()
  3.     {
  4.         if(showTime >=0) //血条倒计时展示
  5.         {
  6.             //简单的倒计时
  7.             showTime -= Time.deltaTime;
  8.             //世界坐标转屏幕坐标
  9.             SreecPosition = Camera.main.WorldToScreenPoint(transform.position);
  10.             //屏幕坐标转GUI坐标_____GUI的原点在左上角,屏幕坐标原点在左下角
  11.             SreecPosition.y = Screen.height - SreecPosition.y;
  12.             //底图Rect位置参数赋值
  13.             GUIPosition1.x = SreecPosition.x - 50;
  14.             GUIPosition1.y = SreecPosition.y - 70;
  15.             GUIPosition1.width = 100;
  16.             GUIPosition1.height = 15;
  17.             //血条图Rect位置参数赋值
  18.             GUIPosition2.x = SreecPosition.x - 50;
  19.             GUIPosition2.y = SreecPosition.y - 70;
  20.             //血条长度和血量同步
  21.             GUIPosition2.width = 100*(nowBlood/maxBlood);
  22.             GUIPosition2.height = 15;
  23.             //绘制底图   
  24.             GUI.DrawTexture(GUIPosition1, underTexture);
  25.            
  26.             //绘制血条      
  27.             GUI.DrawTexture(GUIPosition2, topTexture);
  28.         }
  29.       
  30.     }
复制代码

   更新敌方坦克
  ______-
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. //-------------------------------------
  5. //—————————————————————————————————————
  6. //___________项目:      
  7. //___________功能:敌方坦克逻辑封装
  8. //___________创建者:秩沅_______________
  9. //_____________________________________
  10. //-------------------------------------
  11. public class EnemyTank:TankFather
  12. {
  13.     //随机移动点的位置数组
  14.     public  Transform[] randomPosition;
  15.     //目标位置
  16.     private Transform target;
  17.     //目标坦克
  18.     public Transform Player;
  19.     //检测范围
  20.     public float distance = 10f;
  21.     //子弹发射参数
  22.     public AudioSource shootMusic;   //发射音效
  23.     public GameObject shootball;     //发射的子弹类型
  24.     public float shootSpeed = 1000f; //子弹的速度
  25.     public Transform[] shootTransform; //发射的组件信息
  26.     public bool shootSwitch = false;  
  27.     private float endTime = 3f;      //子弹发射间隔的时间
  28.     //血条的参数
  29.     public Texture underTexture;
  30.     public Texture topTexture;
  31.     private Vector3 SreecPosition;
  32.     private Rect GUIPosition1;
  33.     private Rect GUIPosition2;
  34.     public float showTime = 0;
  35.     private void Start()
  36.     {
  37.         //属性初始化赋值
  38.         
  39.         maxBlood = 500;
  40.         nowBlood = 500;
  41.         attack = 30;
  42.         HeadSpeed = 50;
  43.         //射击音效关闭
  44.         shootMusic.enabled = false;
  45.         moveSpeed = 10f;
  46.         //先随机整一个目标点
  47.         RandomPosition();
  48.     }
  49.     private void Update()
  50.     {   
  51.         transform.LookAt(target);
  52.         //始终超自己的正方向移动
  53.         transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);  //当距离差不多相等时,再随机目标点
  54.         if(Vector3.Distance(transform .position ,target .position)< 0.5f)
  55.         {
  56.             RandomPosition();
  57.         }
  58.             
  59.         //范围检测
  60.         if (Vector3 .Distance(transform .position ,Player.position )<= distance && Player !=null )
  61.         {
  62.             //炮口瞄准玩家
  63.             Head.transform.LookAt(Player);
  64.             //倒计时发射子弹
  65.             endTime = Mathf.MoveTowards(endTime, 0, 0.1f);
  66.             if (endTime <= 0)
  67.             {
  68.                 Fire();
  69.                 endTime = 3f;
  70.             }        
  71.         }
  72.      
  73.     }
  74.     //随机指向一个移动点
  75.     public void RandomPosition()
  76.     {
  77.         if (randomPosition.Length != 0)
  78.             target = randomPosition[Random.Range(0, randomPosition.Length)];
  79.     }
  80.     //触发检测
  81.     private void OnTriggerEnter(Collider other)
  82.     {
  83.       
  84.         if (other.gameObject.tag == "bullet" )
  85.         {
  86.             //添加子弹挂载的目标坦克,方便获取该子弹的攻击力,与受伤逻辑相结合
  87.             BulletMove ball = other.gameObject.GetComponent<BulletMove>();
  88.             TankFather ballTank = ball.Tank.GetComponent<TankFather>();
  89.             //当子弹不是自己打的到自己身上的时候
  90.             if (ballTank.tag != gameObject.tag)
  91.             {
  92.                 //扣血
  93.                 Heart(ballTank);
  94.             }
  95.         }
  96.     }
  97.     public override void Fire()
  98.     {
  99.         //开启射击音效
  100.         shootMusic.enabled = true;
  101.         shootMusic.Play();
  102.         for (int i = 0; i < shootTransform.Length ; i++)
  103.         {
  104.             GameObject ball = Instantiate(shootball, shootTransform[i].position, shootTransform[i].rotation);
  105.             BulletMove movScript = ball.GetComponent<BulletMove>();
  106.             Rigidbody shootBall = ball.GetComponent<Rigidbody>();
  107.             shootBall.AddForce(shootTransform[i].transform.forward * shootSpeed);
  108.             movScript.Tank = gameObject;  //声明子弹是由谁打出去的
  109.         }
  110.      
  111.     }
  112.    
  113.     //死亡行为重写
  114.     public override void Death()
  115.     {
  116.         base.Death();
  117.         GamePlane.SingleInstance.AddScore(10);
  118.     }
  119.     //受伤行为重写
  120.     public override void Heart(TankFather other)
  121.     {
  122.         base.Heart(other);
  123.         showTime = 3; //血条倒计时展示
  124.     }
  125.     //GUI生命函数
  126.     private void OnGUI()
  127.     {
  128.         if(showTime >=0) //血条倒计时展示
  129.         {
  130.             //简单的倒计时
  131.             showTime -= Time.deltaTime;
  132.             //世界坐标转屏幕坐标
  133.             SreecPosition = Camera.main.WorldToScreenPoint(transform.position);
  134.             //屏幕坐标转GUI坐标_____GUI的原点在左上角,屏幕坐标原点在左下角
  135.             SreecPosition.y = Screen.height - SreecPosition.y;
  136.             //底图Rect位置参数赋值
  137.             GUIPosition1.x = SreecPosition.x - 50;
  138.             GUIPosition1.y = SreecPosition.y - 70;
  139.             GUIPosition1.width = 100;
  140.             GUIPosition1.height = 15;
  141.             //血条图Rect位置参数赋值
  142.             GUIPosition2.x = SreecPosition.x - 50;
  143.             GUIPosition2.y = SreecPosition.y - 70;
  144.             //血条长度和血量同步
  145.             GUIPosition2.width = 100*(nowBlood/maxBlood);
  146.             GUIPosition2.height = 15;
  147.             //绘制底图   
  148.             GUI.DrawTexture(GUIPosition1, underTexture);
  149.            
  150.             //绘制血条      
  151.             GUI.DrawTexture(GUIPosition2, topTexture);
  152.         }
  153.       
  154.     }
  155. }
复制代码

相关文章

⭐【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/132032275
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则