前言
欢迎来到【制作100个Unity游戏】系列!本系列将引导您一步步学习如何使用Unity开发各种类型的游戏。在这第23篇中,我们将探索如何制作一个类似于七日杀和森林的生存游戏。
本篇内容会比较多,我会分几篇来实现,感兴趣的可以关注一下,以免错过内容更新。
本节主要实现了可使用的消耗品物品,并提示玩家各项属性的功能。
配置可使用物品功能
下载一些水果模型
https://sketchfab.com/3d-models/lowpoly-fruits-vegetables-d3be8fed96eb48be88b47bbe8d2951e1
这里可以适当增大物品的阻力,让物品可以滚动的同时,又不会滚远
代码实现使用物品
修改InventoryItem,按鼠标右键使用物品
public class InventoryItem : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler { [Header("是否是消耗品")] public bool isConsumable; [Header("对玩家健康值的影响")] public float healthEffect; [Header("对玩家卡路里值的影响")] public float caloriesEffect; [Header("对玩家水分值的影响")] public float hydrationEffect; //。。。 // 当鼠标点击物品时触发 public void OnPointerDown(PointerEventData eventData) { // 右键点击 if (eventData.button == PointerEventData.InputButton.Right) { if (isConsumable) { // 设置待销毁的游戏对象 itemPendingConsumption = gameObject; consumingFunction(healthEffect, caloriesEffect, hydrationEffect); // 消耗品使用效果计算 } } } // 当鼠标释放物品时触发 public void OnPointerUp(PointerEventData eventData) { if (eventData.button == PointerEventData.InputButton.Right) { if (isConsumable && itemPendingConsumption == gameObject) { Destroy(gameObject); // 销毁物品对象 // Destroy(gameObject); InventorySystem.Instance.ReCalculeList(); // 重新计算物品列表 CraftingSystem.Instance.RefreshNeededItems(); // 刷新制作系统所需物品列表 } } } private void consumingFunction(float healthEffect, float caloriesEffect, float hydrationEffect) { itemInfoUI.SetActive(false); // 关闭物品信息UI显示 healthEffectCalculation(healthEffect); // 计算玩家健康值影响 caloriesEffectCalculation(caloriesEffect); // 计算玩家卡路里值影响 hydrationEffectCalculation(hydrationEffect); // 计算玩家水分值影响 } private static void healthEffectCalculation(float healthEffect) { float currentHealth = PlayerState.Instance.currentHealth; float maxHealth = PlayerState.Instance.maxHealth; if (healthEffect != 0) { if ((currentHealth + healthEffect) > maxHealth) { PlayerState.Instance.setHealth(maxHealth); } else { PlayerState.Instance.setHealth(currentHealth + healthEffect); } } } private static void caloriesEffectCalculation(float caloriesEffect) { float currentCalories = PlayerState.Instance.currentCalories; float maxCalories = PlayerState.Instance.maxCalories; if (caloriesEffect != 0) { if ((currentCalories + caloriesEffect) > maxCalories) { PlayerState.Instance.setHealth(maxCalories); } else { PlayerState.Instance.setHealth(currentCalories + caloriesEffect); } } } private static void hydrationEffectCalculation(float hydrationEffect) { float currentHydrationPercent = PlayerState.Instance.currentHydrationPercent; float maxHydrationPercent = PlayerState.Instance.maxHydrationPercent; if (hydrationEffect != 0) { if ((currentHydrationPercent + hydrationEffect) > maxHydrationPercent) { PlayerState.Instance.setHealth(maxHydrationPercent); } else { PlayerState.Instance.setHealth(currentHydrationPercent + hydrationEffect); } } } }
修改PlayerState
public void setHealth(float newHealth) { currentHealth = newHealth; } public void setCalories(float newCalories) { currentCalories = newCalories; } public void setHydration(float newHydration) { currentHydrationPercent = newHydration; }
挂载脚本,配置参数
效果
源码
源码不出意外的话我会放在最后一节