这是第一个 Unity 开放项目的repo,是 Unity 和社区合作创建的一个小型开源游戏演示,第一款游戏是一款名为 Chop Chop 的动作冒险游戏。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

33 行
823 B

using UnityEngine;
public class HungerComponent : MonoBehaviour
{
[SerializeField] private float _eatAmount = 10f;
[SerializeField] private float _maxFullness = 100f;
[SerializeField] private float _hungerPerSecond = 1f;
[SerializeField] [Range(0.01f, 0.99f)] private float _isHungryThreshold = 0.4f;
private bool _getHungry;
private float _currentFullness;
public bool IsHungry => _currentFullness <= _maxFullness * _isHungryThreshold;
private void Awake()
{
_currentFullness = _maxFullness;
}
private void Update()
{
if (_getHungry)
_currentFullness = Mathf.Max(_currentFullness - _hungerPerSecond * Time.deltaTime, 0f);
}
public void Eat()
{
_currentFullness = Mathf.Min(_currentFullness + _eatAmount, _maxFullness);
}
public void ToggleHunger(bool value)
{
_getHungry = value;
}
}