Unity - 触摸沿X轴旋转相机

时间:2015-01-09 00:01:20

标签: c# android unity3d

我试图获得一个统一的 C#脚本,它可以在3D环境中围绕X轴旋转相机。目前,它翻转屏幕使我的地形看起来像倒挂。我只是试图让相机在X轴上旋转。以下是我目前的情况。

using UnityEngine;

public class TouchCamera : MonoBehaviour {
Vector2?[] oldTouchPositions = {
    null,
    null
};
Vector2 oldTouchVector;
float oldTouchDistance;

void Update() {
    if (Input.touchCount == 0) {
        oldTouchPositions[0] = null;
        oldTouchPositions[1] = null;
    }
    else if (Input.touchCount == 1) {
        if (oldTouchPositions[0] == null || oldTouchPositions[1] != null) {
            oldTouchPositions[0] = Input.GetTouch(0).position;
            oldTouchPositions[1] = null;
        }
        else {
            Vector2 newTouchPosition = Input.GetTouch(0).position;

            transform.position += transform.TransformDirection((Vector3)((oldTouchPositions[0] - newTouchPosition) * camera.orthographicSize / camera.pixelHeight * 2f));

            oldTouchPositions[0] = newTouchPosition;
        }
    }
    else {
        if (oldTouchPositions[1] == null) {
            oldTouchPositions[0] = Input.GetTouch(0).position;
            oldTouchPositions[1] = Input.GetTouch(1).position;
            oldTouchVector = (Vector2)(oldTouchPositions[0] - oldTouchPositions[1]);
            oldTouchDistance = oldTouchVector.magnitude;
        }
        else {
            Vector2 screen = new Vector2(camera.pixelWidth, camera.pixelHeight);

            Vector2[] newTouchPositions = {
                Input.GetTouch(0).position,
                Input.GetTouch(1).position
            };
            Vector2 newTouchVector = newTouchPositions[0] - newTouchPositions[1];
            float newTouchDistance = newTouchVector.magnitude;

            transform.position += transform.TransformDirection((Vector3)((oldTouchPositions[0] + oldTouchPositions[1] - screen) * camera.orthographicSize / screen.y));
            transform.localRotation *= Quaternion.Euler(new Vector3(0, 0, Mathf.Asin(Mathf.Clamp((oldTouchVector.y * newTouchVector.x - oldTouchVector.x * newTouchVector.y) / oldTouchDistance / newTouchDistance, -1f, 1f)) / 0.0174532924f));
            camera.orthographicSize *= oldTouchDistance / newTouchDistance;
            transform.position -= transform.TransformDirection((newTouchPositions[0] + newTouchPositions[1] - screen) * camera.orthographicSize / screen.y);

            oldTouchPositions[0] = newTouchPositions[0];
            oldTouchPositions[1] = newTouchPositions[1];
            oldTouchVector = newTouchVector;
            oldTouchDistance = newTouchDistance;
           }
       }
    }
}

2 个答案:

答案 0 :(得分:0)

如果你想旋转它(就像转动它的头部一样),在x-z平原上,你想围绕y轴而不是x旋转它。

答案 1 :(得分:0)

最后,我不得不将光线投射到相机的中心,当它与某些物体相撞时,将其设置为枢轴点。然后我围绕所述枢轴点旋转相机。以下示例......

RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, 300)){}
   float distanceToGround = hit.distance;
   var pivotPoint = hit.point;

// -------- Rotation ---------
transform.RotateAround(pivotPoint, Vector3.up,Mathf.Asin(Mathf.Clamp((oldTouchVector.y * newTouchVector.x - oldTouchVector.x * newTouchVector.y) / oldTouchDistance / newTouchDistance, -1f, 1f)) / 0.0174532924f);

//---------------------------------------------------------