Unity C#使用Transform.RotateAround触摸可以将相机移动到目标周围

时间:2016-07-21 18:38:33

标签: c# camera rotation touch unity5

所以我正在考虑更改用于触摸输入的Transform.RotateAround脚本。如果我摆脱了" Input.GetAxis",相机会自动围绕目标旋转。我似乎无法弄清楚如何格式化代码以使触摸的位置与字符串一起使用。如果有人有任何提示会很棒!

以下是代码:

using UnityEngine;
using System.Collections;

[AddComponentMenu("Camera-Control/Mouse Orbit with zoom2")]
public class MouseOrbitImproved2 : MonoBehaviour {

public float speed; 
public Transform target;
public float rotateSpeed;
public Transform camera = Camera.main.transform;
public Vector3 camPosition;
public float camSpeed;
public float minDistance;
public float maxDistance;
//private Vector3 moveDirection = Vector3.zero;
//private Vector3 moveDirection = target.position;
public float perspectiveZoomSpeed = 0.5f;        // The rate of change     of the field of view in perspective mode.
public float orthoZoomSpeed = 0.5f;        // The rate of change of the orthographic size in orthographic mode.

void start() {

    //camPosition = camera.transform.position;
}

public void Update() {
    if (Input.touchCount == 1) {
        transform.LookAt(target);
        Touch touchSwipe = Input.GetTouch(0);
        string position = touchSwipe.deltaPosition; 

        transform.RotateAround(target.position, Vector3.up, Input.GetAxis(position)* speed);
        transform.RotateAround(target.position, Vector3.forward, Input.GetAxis(position)* speed);
    }

        transform.LookAt(target);
    /*
        float scroll = Input.GetAxis("Mouse ScrollWheel");
        if (scroll != 0)
        {
            // calculate new position first...
            camPosition += transform.forward * scroll * camSpeed;
            // then compare to the limits:
            float distanceToTarget = Vector3.Distance(target.position, camPosition);
            // you can clamp the movement to min and max distances:
            if (distanceToTarget > maxDistance){ // clamp at maxDistance...
                camPosition = target.position - maxDistance * transform.forward;
            }
            if (distanceToTarget < minDistance){ // or at minDistance
                camPosition = target.position - minDistance * transform.forward;
            }
            // finally, update the actual camera position:
            transform.position = camPosition;
        // set camera position
        }
        */

    // If there are two touches on the device...
    if (Input.touchCount == 2)
    {
        // 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;

        // If the camera is orthographic...
        if (Camera.main.orthographic)
        {
            // ... change the orthographic size based on the change in distance between the touches.
            Camera.main.orthographicSize += deltaMagnitudeDiff * orthoZoomSpeed;

            // Make sure the orthographic size never drops below zero.
            Camera.main.orthographicSize = Mathf.Max(Camera.main.orthographicSize, 0.1f);
        }
        else
        {
            // Otherwise change the field of view based on the change in distance between the touches.
            Camera.main.fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed;

            // Clamp the field of view to make sure it's between 0 and 180.
            Camera.main.fieldOfView = Mathf.Clamp(Camera.main.fieldOfView, 0.1f, 179.9f);
        }


        }
}

public void ChangeToAxial (){
    Camera.main.transform.position = new Vector3 (45.3f,234.5f,66.9f);
}

public void ChangeToSagright (){
    Camera.main.transform.position = new Vector3 (28.13f,76.41f,222.68f);
}

public void ChangeToSagleft (){
    Camera.main.transform.position = new Vector3 (49.36f,66.85f,-93.78f);
}

public void ChangeToCoronal () {
    Camera.main.transform.position = new Vector3 (-71.6f,69.66f,66.29f);
}

}

1 个答案:

答案 0 :(得分:0)

我认为如果你从统一论坛Unit 5 forum rotating camera with touch查看这个问题和答案可能是最好的。它应该与Input.GetTouch而不是Input.GetAxis一起使用。希望有所帮助。请注意,脚本是在Javascript中,但您不需要整个脚本。