【问题标题】:Object rotation is not correct when rotating the camera旋转相机时对象旋转不正确
【发布时间】: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


【解决方案1】:

如果我理解正确的话。你想要一个类似于滑动谷歌地图的行为。只是相机必须旋转,图片必须根据行为调整其位置和旋转。

下面的代码应该做到这一点:

void Update() {
    userInterface.transform.rotation = this.transform.rotation;

    // Project camera to canvas plane
    Vector3 projection = Vector3.ProjectOnPlane(this.transform.position - userInterface.transform.position, userInterface.transform.forward)
        + userInterface.transform.position;

    // Get distance from camera to projected position (basically, distance from point to plane)
    float curDistance = (this.transform.position - projection).magnitude;

    // Correct that distance with desired distance
    userInterface.transform.position += this.transform.forward * (placementDistance - curDistance);
}

【讨论】:

  • 如果光线投射没有击中用户界面,我只想更新用户界面位置。因为我正在从我的相机到用户界面进行一些选择
  • 相机包含一个子指针,可以在用户界面上选择不同的对象,它会旋转以在画布的不同位置进行选择。如果我按照您的代码建议更新所有时间位置,我将无法选择它
  • 您可以保存旋转而不是位置。或者您可以使用第二个摄像头使用平面逻辑在顶部渲染 ui,但我不明白如何在不移动一点的情况下保持与旋转对象的最小距离恒定。
  • transform.position 分配“画布”的尝试无效。输入位置是 { Infinity, NaN, Infinity }。 UnityEngine.Transform:set_position(Vector3) UIPlacerAtCamFront:Update() (在 Assets/UIPlacerAtCamFront.cs:42)
  • 稍微调整一下,但你的代码似乎没有这样做
猜你喜欢
  • 2018-06-14
  • 2018-01-30
  • 1970-01-01
  • 1970-01-01
  • 2018-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-15
相关资源
最近更新 更多