我实现了一个简单的卫生系统。
问题:我的设计出了什么问题?
目标:玩家应该意识到什么时候健康值增加了,并且健康值达到了关键的健康因素(每20点健康值),然后做点什么。
问题:正在达到Player.Update(),第22行,因为Player.reachedCriticalHP()返回true,仅是因为它正在使用尚未计算的Health.health值,因为Health.Update()尚未运行。
示例方案:
玩家的生命值从100开始,得到了+10的治疗。
reachedCriticalHP()将返回true,因为它取决于Health.health值。
请问20是100的除数? 100%20 == 0? ->是的,
问题:Player.Update()第22行中的if语句被评估为真
背景:在物理循环(OnTriggerEntered)期间,修复程序将新的“ diff”值告知健康状况以进行更新。但是实际的Health.health是在Update中计算的,因此是在Game-Logic-Cycle中进行的。
在下一个游戏逻辑循环中,在Health.Update依赖于尚未计算的Health.health之前,将调用Player.Update()。
答案 0 :(得分:0)
是否有任何原因导致您在致电Health.Increase()
时不立即更新健康状况?
我不确定您如何设计游戏的其余部分,但是如果在运行Increase()
之前调用任何其他脚本Health.Update()
,它将破坏先前的调用者值。
我的建议是使用Increase()
方法更新运行状况。所以看起来像这样:
void Increase(int amt)
{
if (!isValidDamage(amt)) return;
// this is basically the same as Update()
health = Mathf.Clamp(health + amt, minHealth, maxHealth);
}
如果您想检测健康状况的增加并执行与criticalHealthFactor
所述的操作,则可以创建一个动作或事件并委派并订阅健康状况的更改。看起来像这样:
public Action<int> OnHealthChange;
void Increase(int amt)
{
if (!isValidDamage(amt)) return;
// this is basically the same as update
health = Mathf.Clamp(health + amt, minHealth, maxHealth);
OnHealthChange?.Invoke(health);
}
// this would be in Player.Start()
hp.OnHealthChange += () =>
{
if (!hp.isDead() && reachedCriticalHP())
{
// do something when at critical HP
}
}