在统一中移动摄像机存在问题
本文关键字:摄像机 存在 问题 移动 | 更新日期: 2023-09-27 18:16:22
我的问题是我想把我的相机指向地面,但仍然水平移动它。问题是当我向前移动相机时,因为它已经指向地面的角度,它的局部z轴穿过地面。所以当你向前移动相机时,它会沿着这个轴下降到地面。
我如何将相机指向地面,但保持水平轴?
我有unity版本3.4,它不是专业的,我在c#编码。
感谢任何帮助,因为我刚刚开始尝试学习统一
我假设你使用Camera.transform.translate?如果是这样,修改脚本如下:
Vector3 pos = Camera.transform.position;
pos += new Vector3(1,0,1); //Translate 1 unit on x, and 1 unit on z
Camera.transform.position = pos;
对于更完整的示例,下面是我的MouseLook()代码:
void MoveCamera(){
Vector3 oPos = this.transform.position;
Vector3 newPos = this.transform.position + Translation;
Vector3 forward = Camera.main.transform.forward;
Vector3 sideways = Camera.main.transform.right;
Vector3 up = Camera.main.transform.up;
newPos = oPos + forward * Translation.z;
newPos = newPos + sideways * Translation.x;
if(!_isMouseLook){
//not mouse look so reset position to original height.
//Still apply a Translation as it is tied to the mouse wheel.
newPos.y = oPos.y + Translation.y;
} else {
newPos.y = newPos.y + Translation.y;
}
//Clamp height between terrain floor + camera offset and some max height C.
newPos.y = Mathf.Clamp(newPos.y,Terrain.activeTerrain.SampleHeight(oPos),MaxHeight);
this.transform.position = newPos;
//Reset translation values
Translation = new Vector3(0,0,0);
}
这并没有包含我所有的代码但是我想你们已经明白了要点