簡體   English   中英

Unity C#通過觸摸使用Transform.RotateAround在目標周圍移動相機

[英]Unity C# use Transform.RotateAround with touch to move camera around target

因此,我正在考慮更改我的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);
}

}

我認為,如果您在團結論壇Unit 5論壇觸摸式攝像頭中查看此問題和解答,可能是最好的選擇。 它應該使用Input.GetTouch而不是Input.GetAxis。 希望能有所幫助。 請注意,腳本是用Javascript編寫的,但是您不需要整個腳本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM