我正在尝试通过拖动屏幕在Android设备上旋转相机。拖动水平会将相机移动到左或右和垂直 上或下。 Z轴应该被忽略,但是如果我在屏幕上进行对角线拖动,它将使相机围绕Z轴旋转,因此有时我的相机会处于上下颠倒的位置。
这是我的代码,来自Update()方法:
if (touch.phase == TouchPhase.Moved)
{
float x = touch.deltaPosition.x * rotationSensitivity * Time.deltaTime;
float y = touch.deltaPosition.y * rotationSensitivity * Time.deltaTime;
_camera.transform.Rotate(new Vector3(1, 0, 0), y, Space.Self);
_camera.transform.Rotate(new Vector3(0, -1, 0), x, Space.Self);
}
您使用了错误的transform.Rotate
重载
您正在使用的重载的第一个Vector3
自变量是axis
的旋转位置。
我相信您的意思是提供方向而不是轴,例如:
if (touch.phase == TouchPhase.Moved)
{
Vector2 rotation = (Vector2)touch.deltaPosition * rotationSensitivity * Time.deltaTime;
_camera.transform.Rotate(Vector3.right * rotation.x, Space.Self);
_camera.transform.Rotate(-Vector3.up * rotation.y, Space.Self);
}
因为此代码未经我测试,所以我也建议尝试一下:
if (touch.phase == TouchPhase.Moved)
{
Vector2 rotation = (Vector2)touch.deltaPosition * rotationSensitivity * Time.deltaTime;
_camera.transform.Rotate(transform.right * rotation.x, Space.World);
_camera.transform.Rotate(-transform.up * rotation.y, Space.World);
}
编辑:我将x
和y
混合在一起,已修复。
我在这里找到了类似问题的解决方案:https://gamedev.stackexchange.com/questions/136174/im-rotating-an-object-on-two-axes-so-why-does-it-keep-twisting-around-the-thir
void Update() {
float speed = lookSpeed * Time.deltaTime;
transform.Rotate(0f, Input.GetAxis("Horizontal") * speed, 0f, Space.World);
transform.Rotate(-Input.GetAxis("Vertical") * speed, 0f, 0f, Space.Self);}