赞
踩
1、3D的话就得把血条固定在模型头上面;
2、每一帧都得根据模型的旋转来调整血条,使血条始终朝着摄像机,这样才不会导致血条旋转、缩放等问题;
3、界面同步显示血条;
1、创建Canvas将Canvas
的RenderMode
调整为World Space,
并且将Canvas
作为模型的子节点。
2、创建progress
3、朝向相机
- public class CHpCanvas : MonoBehaviour
- {
- private void Update()
- {
- transform.rotation = Camera.main.transform.rotation;
- }
- }
内容待测试
- public class CHpBar : MonoBehaviour
- {
-
- private Slider hpSlider;
- private RectTransform rectTrans;
-
- public Transform target;
- public Vector3 offsetPos; //头顶偏移量
-
-
- private void Start()
- {
- hpSlider = GetComponent<Slider>();
- rectTrans = GetComponent<RectTransform>();
-
- //更新血量
- //hpSlider.value
- }
-
- private void Update()
- {
- if(target==null) return;
-
- //通过Collider来获取头顶坐标
- var col = target.GetComponent<Collider>();
- var topAhcor = new Vector3(col.bounds.center.x, col.bounds.max.y, col.bounds.center.z);
- //加上头顶偏移量
- Vector3 tarPos = topAhcor;
-
- var viewPos = Camera.main.WorldToViewportPoint(tarPos); //得到视窗坐标
-
-
- Vector2 screenPos;
-
- if (viewPos.z > 0f && viewPos.x > 0f && viewPos.x < 1f && viewPos.y > 0f && viewPos.y < 1f)
- {
-
- //获取屏幕坐标
- screenPos = Camera.main.WorldToScreenPoint(tarPos+offsetPos); //加上头顶偏移量
- }
- else
- {
- //不在可视窗口的模型,把名字移动到视线外
- screenPos = Vector3.up * 3000f;
- }
-
-
- //转化为屏幕坐标
- rectTrans.position = screenPos;
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。