【发布时间】:2014-07-10 23:33:13
【问题描述】:
我有一个有效的 [某种] 健康栏,当敌人追赶你时会显示。唯一的问题是它出现在敌人脚下,偏离中心。我希望酒吧位于敌人头部上方的中心。
我知道问题出在哪里,但不知道如何解决。
public float maxHealth;
public float curHealth;
public Texture2D healthBar;
private float left;
private float top;
private Vector2 playerScreen;
public Fighter player;
public Mob target;
public float healthPercent;
void Start ()
{
maxHealth = 10;
curHealth = maxHealth;
}
void Update ()
{
if(player.opponent != null) {
target = player.opponent.GetComponent<Mob>();
healthPercent = (float)target.health / (float)target.maxHealth;
} else {
target = null;
healthPercent = 0;
}
playerScreen = Camera.main.WorldToScreenPoint(target.transform.position);
left = playerScreen.x; //pretty sure right here
top = (Screen.height - playerScreen.y); //is the issue
}
void OnGUI()
{
if (target != null) {
GUI.DrawTexture(new Rect(left, top, (50 * healthPercent), 5), healthBar);
}
}
【问题讨论】:
-
这是 2d 还是 3d 游戏?你应该查一下worldtoscreenpoint,我认为它是......那么这只是将你的健康条玩家位置减半的问题......
标签: c# user-interface unity3d