做一个3D游戏,要给游戏中的人物头顶上做一个跟随的血条,出现如下问题:
当改变屏幕的分辨率时,血条位置发生巨大偏移。求大神解决。
一下是我unity 工程里的一些参数
Canvas 参数:
UI Scale Mode Scale With Screen Size
Reference Resolution : X:960 Y:640
Screen Match Mode : Match Width Or Height
Match :1
以下是血条跟随人物的代码:
void Update () {
if (target == null)//target 是被跟随的角色
Destroy (this.gameObject);
else {
pos = target.transform.position;//角色世界坐标
pos = Camera.main.WorldToScreenPoint (pos);//角色屏幕坐标
pos = pos * 640 / Screen.height;//适配,为什么这条语句不能达到适配的目的
pos.x -= Screen.width >> 1;
pos.y -= Screen.height >> 1;
this.transform.localPosition = pos;
}
}
如果我讲得不清楚,欢迎加我QQ 497262647 来为我解答,感激不尽