c# - 统一移动相机的问题

标签 c# camera unity3d

我的问题是我想将我的相机指向地面但仍然水平移动它。问题是当我向前移动相机时,因为它已经指向地面一个角度,所以它的局部 z 轴穿过地面。因此,当您向前移动相机时,它会沿着该轴下降到地面。

如何将相机指向地面但保持水平轴?

我有 unity 3.4 版本,它不是专业版,我正在用 C# 编写代码。

感谢任何帮助,因为我刚刚开始尝试学习 unity。

最佳答案

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

这不包含我的所有代码,但我认为您已经掌握了要点。

关于c# - 统一移动相机的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8334971/

相关文章:

c# - 编译表达式树的性能

c# - 是否将 Task<>.Factory.StartNew/Task.Factory.ContinueWhenAll() 转换为 async/await?

ios - ionic 3 : File_URI Not allowed to load local resource IOS

c# - 如何使 Unity 单例基类支持泛型?

c# - 如何在 Android Player 设置中使用 Stripping Level/IL2CPP 选项 Unity

unity3d - Unity Performance - Coroutines vs FSM on update

c# - 获取枚举器 : return or yield return

c# - 在 C# 中给定 LDAP 服务器详细信息,确定用户的用户组/声明

ios - 使用适用于 iOS 的 Adob​​e Builder (flex) 拍照

java - OpenGL/着色器/矩阵 : Weird scaling of the shape after a rotation of the view matrix