移动相机统一的问题

时间:2011-12-01 00:15:27

标签: c# camera unity3d

我的问题是我想将相机指向地面但仍然水平移动。问题是当我向前移动摄像机时,因为它指向地面一个角度,它的局部z轴穿过地面。因此,当您向前移动相机时,它会跟随此轴并下降到地面。

如何将相机对准地面但保持水平轴?

我有统一版本3.4它不是专业版,我用C#编码。

任何帮助表示感谢,因为我刚开始尝试学习团结。

1 个答案:

答案 0 :(得分:3)

我假设您正在使用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);
}

这不包含我的所有代码,但我认为你得到了要点。