当前位置: 首页 > news >正文

中山网页网站设计模板一个网站做多有几种颜色

中山网页网站设计模板,一个网站做多有几种颜色,海尔网站推广策划方案,专业电子科技网站建设本节最终效果演示 文章目录 本节最终效果演示系列目录前言生命 食物 水简单绘制UI玩家状态脚本生命值控制饱食度控制水分控制 源码完结 系列目录 前言 欢迎来到【制作100个Unity游戏】系列!本系列将引导您一步步学习如何使用Unity开发各种类型的游戏。在这第23篇中…

本节最终效果演示

在这里插入图片描述

文章目录

  • 本节最终效果演示
  • 系列目录
  • 前言
  • 生命 食物 水
    • 简单绘制UI
    • 玩家状态脚本
    • 生命值控制
    • 饱食度控制
    • 水分控制
  • 源码
  • 完结

系列目录

前言

欢迎来到【制作100个Unity游戏】系列!本系列将引导您一步步学习如何使用Unity开发各种类型的游戏。在这第23篇中,我们将探索如何制作一个类似于七日杀和森林的生存游戏。

本篇内容会比较多,我会分几篇来实现,感兴趣的可以关注一下,以免错过内容更新。

本节主要实现了玩家生命 食物 水状态控制的功能。

生命 食物 水

简单绘制UI

在这里插入图片描述

玩家状态脚本

public class PlayerState : MonoBehaviour
{public static PlayerState Instance { get; set; } // 单例对象[Header("玩家的健康状态")] public float currentHealth; // 当前生命值public float maxHealth; // 最大生命值[Header("玩家的饱食度状态")]public float currentCalories; // 当前饱食度public float maxCalories; // 最大饱食度[Header("玩家的水分状态")]public float currentHydrationPercent; // 当前水分百分比public float maxHydrationPercent; // 最大水分百分比private void Awake(){if (Instance == null){Instance = this;}else{Destroy(gameObject);}}private void Start(){currentHealth = maxHealth;}
}

挂载脚本,配置参数
在这里插入图片描述

生命值控制

新增HealthBar脚本

public class HealthBar : MonoBehaviour
{private Image slider; // 用于显示血条的图片组件public TextMeshProUGUI healthCounter; // 用于显示当前生命值的文本组件private float currentHealth; // 当前生命值private float maxHealth; // 最大生命值void Awake(){slider = GetComponent<Image>();}void Update(){currentHealth = PlayerState.Instance.currentHealth; // 获取当前生命值maxHealth = PlayerState.Instance.maxHealth; // 获取最大生命值float fillValue = currentHealth / maxHealth; // 计算血条的填充值slider.fillAmount = fillValue; // 设置填充的值healthCounter.text = currentHealth + "/" + maxHealth; // 设置生命值文本}
}

挂载脚本,配置参数
在这里插入图片描述

运行效果
在这里插入图片描述

饱食度控制

修改PlayerState

[Header("玩家的饱食度状态")]
public float currentCalories; // 当前饱食度
public float maxCalories; // 最大饱食度
float distanceTravelled = 0;// 已行进距离
Vector3 lastPosition;// 上一帧位置
public GameObject playerBody;// 玩家角色对象private void Start()
{currentHealth = maxHealth;currentCalories = maxCalories;
}private void Update()
{//根据行进距离扣除饱食度distanceTravelled += Vector3.Distance(playerBody.transform.position, lastPosition); // 计算已行进距离lastPosition = playerBody.transform.position;// 更新上一帧位置if (distanceTravelled >= 5)// 当已行进距离超过5时{distanceTravelled = 0;// 重置已行进距离currentCalories -= 1;// 减少饱食度}
}

配置参数
在这里插入图片描述

新增CaloriesBar,控制饱食度状态栏

public class CaloriesBar : MonoBehaviour
{public TextMeshProUGUI caloriesCounter;private Image slider;private float currentCalories;private float maxCalories;void Awake(){slider = GetComponent<Image>();}void Update(){currentCalories = PlayerState.Instance.currentCalories;maxCalories = PlayerState.Instance.maxCalories;float fillValue = currentCalories / maxCalories;slider.fillAmount = fillValue;caloriesCounter.text = currentCalories + "/" + maxCalories;}
}

配置
在这里插入图片描述
效果
在这里插入图片描述

水分控制

修改PlayerState

[Header("玩家的水分状态")]
public float currentHydrationPercent; // 当前水分百分比
public float maxHydrationPercent; // 最大水分百分比private void Start()
{currentHealth = maxHealth;currentCalories = maxCalories;currentHydrationPercent = maxHydrationPercent;StartCoroutine(decreaseHydration());
}//携程扣水分
IEnumerator decreaseHydration()
{while (true){currentHydrationPercent -= 1;yield return new WaitForSeconds(10f);}
}

新增HydrationBar,控制水分显示

public class HydrationBar : MonoBehaviour
{public TextMeshProUGUI hydrationCounter;private Image slider;private float currentHydration;private float maxHydration;void Awake(){slider = GetComponent<Image>();}void Update(){currentHydration = PlayerState.Instance.currentHydrationPercent;maxHydration = PlayerState.Instance.maxHydrationPercent;float fillValue = currentHydration / maxHydration;slider.fillAmount = fillValue;hydrationCounter.text = currentHydration + "%";}
}

配置
在这里插入图片描述

效果
在这里插入图片描述

源码

源码不出意外的话我会放在最后一节

完结

赠人玫瑰,手有余香!如果文章内容对你有所帮助,请不要吝啬你的点赞评论和关注,以便我第一时间收到反馈,你的每一次支持都是我不断创作的最大动力。当然如果你发现了文章中存在错误或者有更好的解决方法,也欢迎评论私信告诉我哦!

好了,我是向宇,https://xiangyu.blog.csdn.net

一位在小公司默默奋斗的开发者,出于兴趣爱好,最近开始自学unity,闲暇之余,边学习边记录分享,站在巨人的肩膀上,通过学习前辈们的经验总是会给我很多帮助和启发!php是工作,unity是生活!如果你遇到任何问题,也欢迎你评论私信找我, 虽然有些问题我也不一定会,但是我会查阅各方资料,争取给出最好的建议,希望可以帮助更多想学编程的人,共勉~

在这里插入图片描述

http://www.yayakq.cn/news/863919/

相关文章:

  • 适合在线做笔试的网站网络运维必备知识
  • 网站 文件验证网站模版怎么做
  • 制作网站 美工seo网页推广
  • 北京建站免费模板wordpress支持多域名
  • 网站后台数字排版该怎么做wordpress 替代品
  • 会计公司网站模板wordpress 一页一屏
  • 小米网站推广方案宁波专业的网站建设团队
  • 有趣的网站名做ppt选小图案的网站
  • 网站管理员怎样管理员权限设置网站开发主要都做些什么
  • 怎样制作网站?建筑建设网站建设
  • 浏览器主页网址大全关键词优化的原则
  • discuz做淘客网站网站建设与管理实训
  • 平台网站制作公司wordpress多种设备网页生成
  • 上海化工网站建设德州鲁企动力网站优化中心
  • 做类似淘宝网站多少钱泉州自助建站
  • 四川省建设局网站网站开发可以开发哪些
  • 免费 网站阿里云免费服务器
  • 个人做理财网站好wordpress 路径中文乱码
  • 花店网站建设个人小结网站权重的重要性
  • 求网站2021给个网址网架生产公司
  • 做a视频网站有哪些企业网站建设公司名称
  • 临汾建设局官方网站移动商城网站开发选择
  • 学院网站建设开题报告清空网站空间
  • 支付招聘网站套餐费用怎么做帐微网站建设代运营
  • 邢台市桥西住房建设局网站网络推销
  • 网站域名实名证明广西新宇建设项目有限公司网站
  • 企业形象网站建设意义自适应网站模版
  • 电商网站建设应用找合伙人做网站
  • asp网站源码破解社区网站建设公司
  • 国外视觉差网站深圳有做网站的吗