【问题标题】:How to make panel show up in front of camera如何让面板出现在镜头前
【发布时间】:2019-07-24 19:25:20
【问题描述】:

我有这个项目,我在其中单击一个对象,然后将向玩家显示一个画布,在画布内选择一个选项。我需要将画布作为世界空间,以便玩家可以移动头部,并且画布将在前面保持静止。问题是,场景周围有大量对象,每次玩家单击对象时我都需要更新画布的位置。

我尝试使用“transform.position”,但它没有按我想要的方式工作。

观察: painel_escolha = 带面板的画布; transform_tela = 相机。

  painel_escolha.transform.position = transform_tela.transform.position;

【问题讨论】:

  • 所以你想让画布在玩家面前Headstable?您的哪些变换指的是画布,哪些指的是播放器? doesn't work the way I wanted 到底是什么意思?

标签: c# unity3d virtual-reality


【解决方案1】:

使用它来移动相机前面的画布/面板并使其面向相机

// move the canvas distance meters in front of the camera
painel_escolha.transform.position = transform_tela.position + transform_tela.transform.forward * distance;
// make the canvas point in the same direction as the camera
painel_escolha.transform.rotation = transform_tela.transform.rotation;

LateUpdate(!所以在处理用户输入以及位置和旋转更改之后)执行此操作使您的面板绝对头部稳定,这意味着它始终保持在用户面前,他不能基本上把目光移开。

通常您宁愿使用某种平滑的 lerping 来代替,例如在做

[Range(0,1)]
public float interpolationRatePosition = 0.5f;

[Range(0,1)]
public float interpolationRateRotation = 0.5f;

privtae Vector3 lastPosition;
private Quaternion lastRotation;

privtae void LateUpdate()
{
    painel_escolha.transform.position = Vector3.Lerp(painel_escolha.transform.position, transform_tela.position, interpolationRatePosition);

    painel_escolha.transform.rotation = Quaterinon.Lerp(painel_escolha.transform.rotation, transform_tela.rotation, interpolationRateRotation);
} 

这基本上在相同的结束位置,但使它看起来更平滑。

【讨论】:

    【解决方案2】:

    画布位置基本上是 2D 屏幕位置。您可以使用 transform.position 获取单击对象的世界位置,但要使画布元素指向该位置(位于其顶部),您需要通过 https://docs.unity3d.com/ScriptReference/Camera.WorldToScreenPoint.html 将该世界位置转换为屏幕位置

    【讨论】:

      猜你喜欢
      • 2011-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-06
      • 2016-07-16
      • 2019-05-28
      • 2022-08-12
      • 2014-03-19
      相关资源
      最近更新 更多