gpt4 book ai didi

c# - 如何修复面向玩家的翻转世界空间 UI?

转载 作者:行者123 更新时间:2023-12-04 08:43:02 25 4
gpt4 key购买 nike

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,但是当我在游戏中尝试它时,它只是翻转了回来。

最佳答案

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 也是如此

关于c# - 如何修复面向玩家的翻转世界空间 UI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64469700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com