【发布时间】:2017-04-18 18:44:03
【问题描述】:
我有一个摄像头,它将 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 对象正确面对我的相机,甚至我的相机正在观看我的 userinteface 对象的开始或结束。如何根据相机旋转正确旋转我的 UI?
【问题讨论】:
-
我建议您将画布上的渲染模式设置为
Screen Space - Overlay。 -
我不确定期望的行为是什么(在第二张图片上)。你介意详细说明一下吗?另外...如果您在使用欧拉角时遇到问题,请尝试
Quaternion.LookAt(),我发现它更直观且更安全。 -
@lggy 我无法进入屏幕空间
-
我正在使用相机指针在画布上进行一些选择,这就是为什么我不能将画布设置为屏幕空间的原因。相机指针将根据相机旋转,并从世界空间UI中进行选择。
-
@MaglethongSpirr 我希望它旋转但在特定距离。当我的相机看起来像画布(用户界面)对象的开始或结束时,它变得非常靠近我的相机,如图 2 所示。当我修复我的 y 运动时,它显示距离相机很远(旋转),如图像 3rd 建议的那样
标签: c# unity3d camera rotation raycasting