答案 0 :(得分:0)
您可以通过垂直栏的localPosition来实现。
这看起来类似于以下内容:
GameObject verticalBar; //the gameobject you want to use
GameObject player; //Assume this is your play that has the health property
Vector3 startPos; //start position of your total healthbar
Vector3 endPos; //end position of your total healthbar
private void UpdateVerticalBarPosition()
{
float normalized = player.currentHealth / player. maxHealth //this normalizes your current health to be between 0 and 1
if(!float IsNaN(normalized))//(optional) makes sure the value is valid.
{
verticalBar.transform.localPosition = Vector3.Lerp(startPos, endPos, normalized);
}
}
此方法将使您当前的HP正常化。这意味着,例如,如果您的玩家在总共100点生命值中还剩下70点生命值,则归一化值为0.7f
,即为其总生命值的70%。
在Vector3.Lerp
和startPos
之间钳位的endPos
中使用此归一化值(此处startPos
将是您的总健康栏的起始位置,因此左侧并以endPos
代表总健康条的结尾位置,因此灰色条的右侧)将垂直条沿健康条放置70%。黄色/橙色条停止的位置应该在同一点。
如果在hp更改后调用此方法一次,它将跳至新值。但是,如果您逐渐受到损害,也可以循环运行以使其平滑地滑过。
或者。如果您为健康栏使用Unity的Slider
组件,则也可以仅使用Slider.handleRect
。在here文档中可以找到,该文档看起来像这样
Slider healthBar; //the slider used for your healthbar
GameObject verticalBar; // the bar you want displayed at the end of the healthbar
Start()
{
healthBar.handleRect = verticalBar;
}
答案 1 :(得分:0)
我解决了这个问题,虽然很简单! 我只是在滑块内添加了垂直条纹理作为填充图像的子元素,并使其保持正确对齐。
欢呼!