在世界空间中变换面板
Transform a Panel in worldspace
我在世界 space 中有一个 canvas。面板是此 canvas 的子项。
我有另一个参考 MainCamera 移动的球体。
我想将图像固定到世界 space 中的 Sphere。以下是我的代码,
public class ClampImage : MonoBehaviour
{
public Camera FirstpersonCamera;
public GameObject panel;
void Update()
{
//get the position of the sphere in the worldspace
Vector3 spherePosition = FirstpersonCamera.WorldToScreenPoint(this.transform.position);
//assign the world position of the sphere to the image
panel.transform.position = spherePosition;
}
}
此脚本已挂接到球体游戏对象。不幸的是,面板根本没有移动。如何将面板夹到球体上?
好吧,它与 Screenspace Overlay
一起工作,因为那时你的行
Vector3 spherePosition = FirstpersonCamera.WorldToScreenPoint(this.transform.position);
有意义并将您的面板放置在像素 space 坐标中。
现在您的面板位于 Worldspace
中,您不想再将 World-Space 位置转换为像素显示 space。所以简单地去掉这一步,而是直接使用你已经拥有的世界space位置
//assign the world position of the sphere to the image
panel.transform.position = transform.position;
然后让它总是看着相机你可以简单地做
panel.transform.rotation = FirstPersonCamera.transform.rotation;
或者,如果您希望它更正确并且不依赖于您正在寻找的方向,您可以使用
panel.transform.LookAt(panel.transform.position + Camera.transform.forward);
因为面板的前向矢量实际上必须完全远离相机。
如果您希望图像有额外的偏移量,例如在球体前你也可以做
panel.transform.position = transform.position - FirstPersonCamera.transform.forward * SomeOffsetInUnits;
我在世界 space 中有一个 canvas。面板是此 canvas 的子项。 我有另一个参考 MainCamera 移动的球体。
我想将图像固定到世界 space 中的 Sphere。以下是我的代码,
public class ClampImage : MonoBehaviour
{
public Camera FirstpersonCamera;
public GameObject panel;
void Update()
{
//get the position of the sphere in the worldspace
Vector3 spherePosition = FirstpersonCamera.WorldToScreenPoint(this.transform.position);
//assign the world position of the sphere to the image
panel.transform.position = spherePosition;
}
}
此脚本已挂接到球体游戏对象。不幸的是,面板根本没有移动。如何将面板夹到球体上?
好吧,它与 Screenspace Overlay
一起工作,因为那时你的行
Vector3 spherePosition = FirstpersonCamera.WorldToScreenPoint(this.transform.position);
有意义并将您的面板放置在像素 space 坐标中。
现在您的面板位于 Worldspace
中,您不想再将 World-Space 位置转换为像素显示 space。所以简单地去掉这一步,而是直接使用你已经拥有的世界space位置
//assign the world position of the sphere to the image
panel.transform.position = transform.position;
然后让它总是看着相机你可以简单地做
panel.transform.rotation = FirstPersonCamera.transform.rotation;
或者,如果您希望它更正确并且不依赖于您正在寻找的方向,您可以使用
panel.transform.LookAt(panel.transform.position + Camera.transform.forward);
因为面板的前向矢量实际上必须完全远离相机。
如果您希望图像有额外的偏移量,例如在球体前你也可以做
panel.transform.position = transform.position - FirstPersonCamera.transform.forward * SomeOffsetInUnits;