Unity UI上的物体跟随场景物体位置变化而变化(人物血条/称号)

首先看下UI上物体的位置计算(UI上的物体 以下用“血条”代称)

这个很简单 无非就是坐标转换 把人物的世界坐标转到屏幕坐标
代码如下:

public Transform target;
    public Transform hpSp;
    public Camera mainCamera;


    void Update() {
        Vector3 pos11 = mainCamera.WorldToScreenPoint(target.position);
        hpSp.transform.localPosition = pos11;
    }

这时候我们运行程序 发现欸嘿 不对啊

这时候 我们发现血条并没有按照我们的想法出现在规定的位置
那么是什么原因导致的呢?
这时候我们发现血条(UI)的坐标系的中心点在屏幕的中间位置(ui坐标) 而我们屏幕的坐标系的中心点是左下角
于是我们需要做一下屏幕坐标转UI坐标
代码如下:

public Transform target;
    public Transform hpSp;
    public Camera mainCamera;


    void Update() {
        Vector3 pos = mainCamera.WorldToScreenPoint(target.position);
        //Screen.width和Screen.height分别为屏幕的宽的像素和屏幕的高的像素
        Vector3 pos1 = new Vector3(pos.x - Screen.width / 2, pos.y - Screen.height / 2, pos.z);
        hpSp.transform.localPosition = pos1;
    }

这时候我们运行程序之后发现血条到了我们规定的位置

这时候 我改动了 我的屏幕分辨率 由1280/720 改为1600/900 然后我发现血条的位置发生了轻微的改动 于是我做了下一步操作
代码如下:

public Transform target;
    public Transform hpSp;
    public Camera mainCamera;


    void Update() {
        Vector3 pos = mainCamera.WorldToScreenPoint(target.position);
        //Screen.width和Screen.height分别为屏幕的宽的像素和屏幕的高的像素
        Vector3 pos1 = new Vector3(pos.x - Screen.width / 2, pos.y - Screen.height / 2, pos.z);
        float scale = 1280f / Screen.width;
        Vector3 screenPos = new Vector3(pos1.x * scale, pos1.y * scale, 0.0f);
        hpSp.transform.localPosition = screenPos;
    }

 注: 我的 1280来源于这里

到了这里我觉得 血条挡在人脸上实在是难看 又懒得去找策划加血条位置的骨骼点 于是决定自己加一个血条距离人物的高度
代码如下:

public Transform target;
    public Transform hpSp;
    public Camera mainCamera;
    public Vector3 offsetPos;

    void Update() {
        Vector3 pos = mainCamera.WorldToScreenPoint(target.position+ offsetPos);
        //Screen.width和Screen.height分别为屏幕的宽的像素和屏幕的高的像素
        Vector3 pos1 = new Vector3(pos.x - Screen.width / 2, pos.y - Screen.height / 2, pos.z);
        float scale = 1280f / Screen.width;
        Vector3 screenPos = new Vector3(pos1.x * scale, pos1.y * scale, 0.0f);
        hpSp.transform.localPosition = screenPos;
    }

完整代码如下:

public class test5 : MonoBehaviour {
    public Transform target;
    public Transform hpSp;
    public Camera mainCamera;
    public Vector3 offsetPos;

    void Update() {
        Vector3 pos = mainCamera.WorldToScreenPoint(target.position+ offsetPos);
        //Screen.width和Screen.height分别为屏幕的宽的像素和屏幕的高的像素
        Vector3 pos1 = new Vector3(pos.x - Screen.width / 2, pos.y - Screen.height / 2, pos.z);
        float scale = 1280f / Screen.width;
        Vector3 screenPos = new Vector3(pos1.x * scale, pos1.y * scale, 0.0f);
        hpSp.transform.localPosition = screenPos;
    }
}

  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一梭键盘任平生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值