Unity Change UI Z-Axis

时间:2016-06-09 09:04:34

标签: unity3d

我正在与一个小团队一起创建一个名为“ArcheryRun”的射箭,平台游戏,射击游戏。我们有一个电源栏(以橙色显示),当您按住“鼠标左键”时,它会增加。但是,由于它是一个UI元素,因此它在某个位置是静态的。

我希望它在玩家更改z轴时显示在玩家上方,就像我使用2D Sprite对象完成箭头图像一样。但是我似乎无法改变UI图像相对于播放器的z轴或使用允许填充选项的游戏对象。

感谢任何帮助,谢谢:)

Change Orange Powerbar to Follow Player

1 个答案:

答案 0 :(得分:0)

如果我们理解您的问题,这应该有效:

此方法为您提供世界位置的屏幕画布位置:

    public static Vector3 GetScreenPositionFromWorldPosition(Vector3 targetPosition)
    {
        Vector3 screenPos;    
        screenPos = Camera.main.WorldToScreenPoint(targetPosition);
        return new Vector3 (screenPos.x, screenPos.y, 0f);
    }

然后你需要一个功能栏顶部的脚本,让我们这样说:

public class PowerBar : MonoBehaviour 
{
    public Transform target;
    RectTransform _rectTransform;

    void Awake ()
    {
        _rectTransform = GetComponent<RectTransform> ();
    }

    public void updatePosition()
    {
        Vector3 pos = YourStaticClassWith.GetScreenPositionFromWorldPosition (target.position);
        Vector2 rpos = pos;
        _rectTransform.anchoredPosition3D = rpos + Vector2.up * 100f; // 100f is value of how many pixels above the target that UI element should apear
    }

    void LateUpdate()
    {
        updatePosition ();
    }
}

target基本上是您的玩家对象的transform

100f值表示目标上方的像素。

相关问题