導(dǎo)航網(wǎng)站怎么做首頁優(yōu)化公司
本節(jié)最終效果演示
文章目錄
- 本節(jié)最終效果演示
- 系列目錄
- 前言
- 生命 食物 水
- 簡單繪制UI
- 玩家狀態(tài)腳本
- 生命值控制
- 飽食度控制
- 水分控制
- 源碼
- 完結(jié)
系列目錄
前言
歡迎來到【制作100個Unity游戲】系列!本系列將引導(dǎo)您一步步學(xué)習(xí)如何使用Unity開發(fā)各種類型的游戲。在這第23篇中,我們將探索如何制作一個類似于七日殺和森林的生存游戲。
本篇內(nèi)容會比較多,我會分幾篇來實(shí)現(xiàn),感興趣的可以關(guān)注一下,以免錯過內(nèi)容更新。
本節(jié)主要實(shí)現(xiàn)了玩家生命 食物 水狀態(tài)控制的功能。
生命 食物 水
簡單繪制UI
玩家狀態(tài)腳本
public class PlayerState : MonoBehaviour
{public static PlayerState Instance { get; set; } // 單例對象[Header("玩家的健康狀態(tài)")] public float currentHealth; // 當(dāng)前生命值public float maxHealth; // 最大生命值[Header("玩家的飽食度狀態(tài)")]public float currentCalories; // 當(dāng)前飽食度public float maxCalories; // 最大飽食度[Header("玩家的水分狀態(tài)")]public float currentHydrationPercent; // 當(dāng)前水分百分比public float maxHydrationPercent; // 最大水分百分比private void Awake(){if (Instance == null){Instance = this;}else{Destroy(gameObject);}}private void Start(){currentHealth = maxHealth;}
}
掛載腳本,配置參數(shù)
生命值控制
新增HealthBar腳本
public class HealthBar : MonoBehaviour
{private Image slider; // 用于顯示血條的圖片組件public TextMeshProUGUI healthCounter; // 用于顯示當(dāng)前生命值的文本組件private float currentHealth; // 當(dāng)前生命值private float maxHealth; // 最大生命值void Awake(){slider = GetComponent<Image>();}void Update(){currentHealth = PlayerState.Instance.currentHealth; // 獲取當(dāng)前生命值maxHealth = PlayerState.Instance.maxHealth; // 獲取最大生命值float fillValue = currentHealth / maxHealth; // 計算血條的填充值slider.fillAmount = fillValue; // 設(shè)置填充的值healthCounter.text = currentHealth + "/" + maxHealth; // 設(shè)置生命值文本}
}
掛載腳本,配置參數(shù)
運(yùn)行效果
飽食度控制
修改PlayerState
[Header("玩家的飽食度狀態(tài)")]
public float currentCalories; // 當(dāng)前飽食度
public float maxCalories; // 最大飽食度
float distanceTravelled = 0;// 已行進(jìn)距離
Vector3 lastPosition;// 上一幀位置
public GameObject playerBody;// 玩家角色對象private void Start()
{currentHealth = maxHealth;currentCalories = maxCalories;
}private void Update()
{//根據(jù)行進(jìn)距離扣除飽食度distanceTravelled += Vector3.Distance(playerBody.transform.position, lastPosition); // 計算已行進(jìn)距離lastPosition = playerBody.transform.position;// 更新上一幀位置if (distanceTravelled >= 5)// 當(dāng)已行進(jìn)距離超過5時{distanceTravelled = 0;// 重置已行進(jìn)距離currentCalories -= 1;// 減少飽食度}
}
配置參數(shù)
新增CaloriesBar,控制飽食度狀態(tài)欄
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("玩家的水分狀態(tài)")]
public float currentHydrationPercent; // 當(dāng)前水分百分比
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 + "%";}
}
配置
效果
源碼
源碼不出意外的話我會放在最后一節(jié)
完結(jié)
贈人玫瑰,手有余香!如果文章內(nèi)容對你有所幫助,請不要吝嗇你的點(diǎn)贊評論和關(guān)注
,以便我第一時間收到反饋,你的每一次支持
都是我不斷創(chuàng)作的最大動力。當(dāng)然如果你發(fā)現(xiàn)了文章中存在錯誤
或者有更好的解決方法
,也歡迎評論私信告訴我哦!
好了,我是向宇
,https://xiangyu.blog.csdn.net
一位在小公司默默奮斗的開發(fā)者,出于興趣愛好,最近開始自學(xué)unity,閑暇之余,邊學(xué)習(xí)邊記錄分享,站在巨人的肩膀上,通過學(xué)習(xí)前輩們的經(jīng)驗(yàn)總是會給我很多幫助和啟發(fā)!php是工作,unity是生活!如果你遇到任何問題,也歡迎你評論私信找我, 雖然有些問題我也不一定會,但是我會查閱各方資料,爭取給出最好的建議,希望可以幫助更多想學(xué)編程的人,共勉~