Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# TransformDirection在创建RTS摄影机时未按预期运行_C#_Unity3d - Fatal编程技术网

C# TransformDirection在创建RTS摄影机时未按预期运行

C# TransformDirection在创建RTS摄影机时未按预期运行,c#,unity3d,C#,Unity3d,我正在尝试使用Unity engine从头开始创建RTS游戏。我从这个开始,我正在设置相机。基本上,我所做的是,生成运动的向量。然后我需要把它应用到相机上 如果相机不旋转,它工作得很好——但它通常是旋转的,必须是可旋转的 根据教程,我可以使用camera.main.transform.TransformDirection(MyVector)将向量转换为摄影机的视点。我就是这么做的: //Get mouse position float xpos = Input.mousePosi

我正在尝试使用Unity engine从头开始创建RTS游戏。我从这个开始,我正在设置相机。基本上,我所做的是,生成运动的向量。然后我需要把它应用到相机上

如果相机不旋转,它工作得很好——但它通常是旋转的,必须是可旋转的

根据教程,我可以使用
camera.main.transform.TransformDirection(MyVector)
将向量转换为摄影机的视点。我就是这么做的:

    //Get mouse position
    float xpos = Input.mousePosition.x;
    float ypos = Input.mousePosition.y;
    //Create vector for movement
    Vector3 movement = new Vector3(0, 0, 0);

    //horizontal camera movement
    if (xpos >= 0 && xpos < ResourceManager.ScrollWidth)
    {
        movement.x -= ResourceManager.ScrollSpeed;
    }
    else if (xpos <= Screen.width && xpos > Screen.width - ResourceManager.ScrollWidth)
    {
        movement.x += ResourceManager.ScrollSpeed;
    }
    //vertical camera movement
    if (ypos >= 0 && ypos < ResourceManager.ScrollWidth)
    {
        movement.z -= ResourceManager.ScrollSpeed;
    }
    else if (ypos <= Screen.height && ypos > (Screen.height - ResourceManager.ScrollWidth))
    {
        movement.z += ResourceManager.ScrollSpeed;
    }

    //make sure movement is in the direction the camera is pointing
    //but ignore the vertical tilt of the camera to get sensible scrolling
    movement = Camera.main.transform.TransformDirection(movement);
    //Up/down movement will be calculated diferently
    movement.y = 0;
//获取鼠标位置
float xpos=Input.mousePosition.x;
float ypos=Input.mousePosition.y;
//创建运动矢量
Vector3移动=新Vector3(0,0,0);
//水平摄像机运动
if(xpos>=0&&xpos=0&&ypos
然而,如果我这样做,垂直运动对初始相机旋转不起作用,当我旋转相机时,运动以奇怪的速度发生


如何在相机上正确应用运动矢量?

这里的问题是TransformDirection将保留矢量的长度。因此,如果您向下倾斜相机,则整个长度将分布在y、x和z轴之间。由于只需消除y部分,向量将变短。相机倾斜的角度越大,在其他两个轴上的损失就越大

旋转单位向量、根据需要修改它并在完成时对其进行规格化通常更容易。然后,可以根据实际的运动矢量来缩放该单位矢量。诸如此类:

// [...]
Vector3 forward = Camera.main.transform.forward;
Vector3 right = Camera.main.transform.right;
forward.y = 0f;
right.y = 0f;  // not needed unless your camera is also rotated around z
movement = forward.normalized * movement.z + right.normalized * movement.x;
// [...]

另一种方法是分开你的两个旋转。因此,通过使用一个只围绕y旋转的空游戏对象,并将相机作为该游戏对象的子对象。现在,相机将仅围绕局部x轴旋转,以按您想要的方式倾斜。若要使相机横向观察另一个方向,请围绕y旋转空游戏对象。这样,您可以使用空游戏对象的变换来变换方向,就像您在代码中所做的那样,因为相机的倾斜无关紧要。

非常感谢您的帮助。还有最后一个小问题:如果我把相机旋转90度以上(所以它是“倒置的”),上/下方向是颠倒的。我将通过将角度限制为最大值来修复它。