【问题标题】:Unity3D Touch Rotate ObjectUnity3D Touch 旋转对象
【发布时间】:2015-04-27 08:57:30
【问题描述】:

我想在不使用z-axis 的情况下旋转我的gameobject,使其只能水平和垂直旋转。

现在我正在使用此代码

void Update () {
    if (Input.touchCount == 1) {
        var touch = Input.GetTouch(0);
        switch(Input.GetTouch(0).phase){
        case TouchPhase.Moved:
            float swipeDistVertical = (new Vector3(0, touch.deltaPosition.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;              
            if (swipeDistVertical > 0)                  
            {                   
                float swipeValue = Mathf.Sign(touch.deltaPosition.y - startPos.y);                   
                if (swipeValue > 0 || swipeValue < 0)//up swipe     
                {
                    vertical = true;
                    horizontal = false;
                }                               
            }               
            float swipeDistHorizontal = (new Vector3(touch.deltaPosition.x,0, 0) - new Vector3(startPos.x, 0, 0)).magnitude;             
            if (swipeDistHorizontal > 0)                    
            {                   
                float swipeValue = Mathf.Sign(touch.deltaPosition.x - startPos.x);                   
                if (swipeValue > 0 || swipeValue < 0)//right swipe
                {
                    horizontal = true;
                    vertical = false;
                }                           
            }

            if(vertical)
            {
                transform.Rotate(touch.deltaPosition.y * 0.3f, 0,0,Space.World);
            }
            if(horizontal)
            {
                transform.Rotate(0,touch.deltaPosition.x * 0.3f,0,Space.World);
            }
            break;
        }
    }
}

我从这个link得到这个代码

现在我可以旋转,但它也在 z 轴上旋转,这是我不想要的。而且它不处理垂直向右滑动,它在两者之间切换,而不是现在识别它是垂直的。

我使用 Unity 4.6.2,这应该适用于 iOSAndroid

【问题讨论】:

    标签: android ios unity3d rotation touch


    【解决方案1】:

    在“break”之前添加以下代码。

    float z=transform.rotation.z;
    transform.Rotate(0,0,-z);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      • 2014-08-05
      • 1970-01-01
      相关资源
      最近更新 更多