我有一个摄像头,它将userInterface(画布)对象显示在我的相机前面。 我只更新我的用户界面位置,如果相机光线不击中我的userInterface对象对撞机。像这样旋转相机时对象旋转不正确
public class UIPlacerAtCamFront : MonoBehaviour {
public GameObject userInterface;
public float placementDistance;
Vector3 uiPlacementPosition;
RaycastHit objectHit;
void Update() {
Vector3 fwd = this.transform.TransformDirection(Vector3.forward);
//Debug.DrawRay(this.transform.position, fwd * 50, Color.green);
if (Physics.Raycast(this.transform.position, fwd, out objectHit, 50))
{
//raycast hitting the userInterface collider, so do nothing, userinterface is infornt of the camrea already
}
else
{
//raycast is not hitting userInterfae collider, so update UserInterface position accoding to the camera
uiPlacementPosition = this.transform.position + this.transform.forward * placementDistance;
userInterface.transform.position = uiPlacementPosition;
}
//Continuously update userInterface rotation according to camera
userInterface.transform.rotation = this.transform.rotation;
}
}
上述脚本已附有我的相机,它正确显示的对象,但我开始转动我的相机我的UI对象的旋转看起来如下图像建议
我知道问题出在旋转,所以我累了改变我的这个轮换代码
userInterface.transform.rotation = this.transform.rotation;
这个
userInterface.transform.localEulerAngles = new Vector3 (this.transform.localEulerAngles.x,
0,
this.transform.localEulerAngles.z);
我想,我的userinteface对象面对我的相机correclty,甚至我的相机看开头或结尾给出我的用户界面对象。如何根据相机旋转正确旋转我的用户界面?
我建议设置渲染模式画布成'屏幕空间上 - Overlay'。 – Iggy
我不确定期望的行为会是什么(在第二张图片上)。你介意一点点介绍一下吗? 另外...如果你有欧拉角度的问题尝试'Quaternion.LookAt()',我觉得它更直观和防弹。 –
@lggy我不能让它的屏幕空间 –