对象在90°和-90°时的旋转误差

时间:2017-01-26 11:21:54

标签: ios unity3d rotation

我正在开发一个应用程序,允许用户使用iOS触摸控件旋转对象。

我有以下脚本正常工作,有一个我似乎无法解决的问题。

GameObject mainCamera;
public Camera camMain;

// One Touch Rotation
public float rotateSpeed = 0.5f;
static float pitch = 0.0f, yaw = 0.0f, zed = 0.0f, pitchBravo = 0.0f, yawBravo = 0.0f;

// Two Touch Zoom
public float perspectiveZoomSpeed = 0.1f;

// Three Touch Pan
public float panSpeed = 0.5f;
private float xAxis = 0.0f, yAxis = 0.0f;
private float xMain, yMain, zMain;

// Game Objects, Public or Private
private GameObject bravo;

void Update()
{
    // Grabs Bravo
    bravo = GameObject.Find ("bravo");

    pitch = bravo.transform.eulerAngles.x;
    yaw = bravo.transform.eulerAngles.y;

    // One Touch controls rotation of Bravo
    if (Input.touchCount == 1) 
    {
        // Retrieves a single touch and names it TouchZero
        Touch touchZero = Input.GetTouch (0);

        // The start of the rotation will be aligned with Bravo's current rotation
        //pitch = bravo.transform.eulerAngles.x;
        //yaw = bravo.transform.eulerAngles.y;

        // Times the difference in position of touch between frames by the rotation speed. deltaTime to keep movement consistent on all devices
        pitch += touchZero.deltaPosition.y * rotateSpeed * Time.deltaTime;
        yaw -= touchZero.deltaPosition.x * rotateSpeed * Time.deltaTime;

        // Assigns the new eulerAngles to Bravo
        bravo.transform.eulerAngles = new Vector3 (pitch, yaw, 0.0f);
    }

    // Two Touch contols the Field of View of the Camera aka. Zoom
    if (Input.touchCount == 2)
    {
        mainCamera = GameObject.Find("main");

        // Store both touches.
        Touch touchZero = Input.GetTouch(0);
        Touch touchOne = Input.GetTouch(1);

        // Find the position in the previous frame of each touch.
        Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
        Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;

        // Find the magnitude of the vector (the distance) between the touches in each frame.
        float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
        float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;

        // Find the difference in the distances between each frame.
        float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;

        // Otherwise change the field of view based on the change in distance between the touches.
        camMain.fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed;

        // Clamp the field of view to make sure it's between 0 and 180.
        camMain.fieldOfView = Mathf.Clamp(camMain.fieldOfView, 2.0f, 30.0f);

        xAxis -= touchOne.deltaPosition.x * panSpeed * Time.deltaTime;
        yAxis -= touchOne.deltaPosition.y * panSpeed * Time.deltaTime;

        zMain = mainCamera.transform.position.z;

        mainCamera.transform.position = new Vector3 (xAxis, yAxis, zMain);

    }

}

使用此脚本,对象可以完全左右旋转360°。但是,当模型旋转上下时,它会达到90°或-90°标记并发出错误,不允许用户继续旋转。

我做了一些研究,我相信它可能与万向节锁有关,我对这个问题缺乏了解意味着我无法想出一个固定。

感谢任何帮助。

GIF of bugged rotation

0 个答案:

没有答案