分析对手网站的优化方法,网站技术解决方案的内容,自己建购物网站,北京工程信息网本节最终效果演示 文章目录 本节最终效果演示系列目录前言生命 食物 水简单绘制UI玩家状态脚本生命值控制饱食度控制水分控制 源码完结 系列目录
前言
欢迎来到【制作100个Unity游戏】系列#xff01;本系列将引导您一步步学习如何使用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 GetComponentImage();}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 GetComponentImage();}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 GetComponentImage();}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是生活如果你遇到任何问题也欢迎你评论私信找我 虽然有些问题我也不一定会但是我会查阅各方资料争取给出最好的建议希望可以帮助更多想学编程的人共勉~