如何在Unity中修复朝向玩家的翻转世界空间UI?

3
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Well : MonoBehaviour
{
    public int currentLiter = 5;
    public int maxWellCapacity = 10;

    public float refillTime = 8f;

    public Text wellDurText;
    public Image refillTimeText;
    public GameObject player;

    public void Update()
    {
        if (currentLiter < maxWellCapacity)
        {
            RefillWell();
        }

        wellDurText.GetComponent<Text>().text = currentLiter.ToString();
        wellDurText.transform.LookAt(player.transform);

        refillTimeText.GetComponent<Image>().fillAmount = refillTime/8;
        refillTimeText.transform.LookAt(player.transform);
    }

    void RefillWell()
    {
        refillTime -= Time.deltaTime;

        if (refillTime <= 0.0f)
        {
            currentLiter += 1;
            refillTime = 8f;
        }
    }
}

我想要做的事情是:让wellDurText无论玩家在哪里都面向玩家,但由于某种原因,它被翻转了。 我尝试过手动从编辑器翻转UI,但当我在游戏中尝试时,它又翻转回来了。

1个回答

4

UI元素forward向量必须指向远离观众的方向。这是因为通常用于2D UI的默认前向方向指向显示器,但您希望将UI赋予用户坐在显示器前面(或者从Unity的角度看是在后面;))。

当您使用

wellDurText.transform.LookAt(player.transform);

你需要让它的 forward 向量指向玩家,这会使它指向完全相反的方向。
例如,应该是这样的:
var direction = wellDurText.transform.position - player.transform.position;
var lookRotation = Quaternion.LookDirection(direction);
wellDurText.transform.rotation = lookRotation;

同样适用于RefillTimeText


非常感谢,这真的很有帮助。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接