在Unity中绕X和Y轴旋转对象问题

问题描述 投票:1回答:2

我正在尝试通过拖动屏幕在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); 

}
android unity3d rotation touch
2个回答
1
投票

您使用了错误的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); 
}

编辑:我将xy混合在一起,已修复。


0
投票

我在这里找到了类似问题的解决方案: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);}
© www.soinside.com 2019 - 2024. All rights reserved.