首页 > 解决方案 > 相机不会使用 Unity3d 中的脚本旋转

问题描述

 if (Input.GetKeyDown(KeyCode.E))
    {
        Camera.main.transform.eulerAngles = new Vector3(0, 180, 0);
    }

此代码应旋转相机,使其面向玩家。它是一种“回顾”功能。问题是,事实并非如此。它只是吓坏了,然后又回到了原来的方向。为什么是这样?

标签: c#unity3d

解决方案


当您按下“E”键时,您并没有旋转游戏对象。当按下“E”键时,您将相机的旋转设置为相同的 180 值。每次按下该键时,它始终为 180。

如果您想在每次按下“E”键时将相机旋转 180 度,则必须增加相机旋转,+=而不是=简单地一遍又一遍地分配 180 度的角度:

void Update()
{
    if (Input.GetKeyDown(KeyCode.E))
    {
        Camera.main.transform.eulerAngles += new Vector3(0, 180, 0);
    }
}

您还可以使用transform.Rotate

void Update()
{
    if (Input.GetKeyDown(KeyCode.E))
    {
        Camera.main.transform.Rotate(new Vector3(0, 180, 0));
    }
}

请注意我是如何使用Update函数 becomeFixedUpdate用于向Rigidbody对象添加物理力的。


推荐阅读