我正在制作一个简单的统一3D健康酒吧。健康栏UI是由图像组成的。我对根据健康百分比来设置酒吧的位置有问题。这里有一个脚本部分:
percentHealth = (currentHealth / maxHealth) * 100; //get's health percentage.
HealthBarHealth.transform.localPosition = new Vector3(healthBarCurrentX, 0); //moves health bar
我希望根据这些值计算healthBarCurrentX:
percentHealth -从最大生命值和当前值计算的百分比。healthBarMaxX = 0f -健康栏的位置为100%健康。healthBarMinX = -273.5f -bar在0%健康的位置。
因此,当健康状况为50%时,currentX为-136.65,等等。
发布于 2015-06-07 21:18:41
使用Mathf.Lerp()在您想要的0%的值和您想要的100%的值之间进行插值。
Mathf.Lerp
healthBarCurrentX = Mathf.Lerp(healthBarMinX, healthBarMaxX, currentHealth / maxHealth);
发布于 2015-06-07 14:43:39
你可以有一个黑色的酒吧,上面涂了一个绿色的条子。只要改变顶部的条子的长度,它就会工作得很好。
blackbarlength = 100;
percentHealth = (currentHealth / maxHealth) * 100;
greenbarlength = percenthealth;
发布于 2015-06-08 14:49:17
为什么不使用uGUI的Slider组件?它非常简单,灵活,分辨率高的independent.Check in,https://unity3d.com/learn/tutorials/projects/survival-shooter/player-health
https://gamedev.stackexchange.com/questions/102086
复制相似问题