我试图创建一个RTS游戏从零使用统一引擎。我从这个统一RTS游戏教程开始,我正在设置摄像机。基本上,我所做的就是,为运动生成[x,0,z]矢量。然后我需要把它用在相机上。
如果相机不是旋转的,它就能完美地工作--但它通常是旋转的,并且必须是可旋转的。
根据还有那些医生教程,我可以使用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;但是,如果我这样做的话,垂直运动对摄像机的旋转就不起作用了,当我旋转相机的时候,这种运动发生在奇怪的速度上。
如何正确地将运动矢量应用到摄像机上?
发布于 2014-11-02 02:32:29
这里的问题是,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;
// [...]另一种方法是分开你的两个轮值。因此,通过使用一个空的gameobject,它只在y周围旋转,并且有相机,它是那个游戏对象的子对象。现在,相机只会围绕局部x轴旋转,以你想要的方式倾斜它。要拍相机,可以将空的GameObject旋转到y周围,这样就可以使用空GameObject的转换来像代码中那样转换方向,因为相机的倾斜并不重要。
https://stackoverflow.com/questions/26693867
复制相似问题