iLMS知識社群歷程檔Login
Position: 黃國哲 > Unity
1090705 Camera操作
by 黃國哲 2020-07-05 14:13:14, Reply(0), Views(628)


原理是對Camera執行動作操作




測試2D可以,但是移動的過程沒有盡頭

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class cameramove : MonoBehaviour
{
    Vector3 touchStart;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButtonDown(0))
          {touchStart = Camera.main.ScreenToWorldPoint(Input.mousePosition);}

        if(Input.GetMouseButton(0))
        {Vector3 direction = touchStart - Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Camera.main.transform.position += direction;}
       // print("null");
    }
}

///////////使用滑鼠滾輪放大縮小Camera

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class cameramove : MonoBehaviour
{
    Vector3 touchStart;
    public float zoomOutMin = 1 ;  //縮放最小
    public float zoomOutMax = 8 ; //縮放最大
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButtonDown(0))
          {touchStart = Camera.main.ScreenToWorldPoint(Input.mousePosition);}

        if(Input.GetMouseButton(0))
        {Vector3 direction = touchStart - Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Camera.main.transform.position += direction;}
        zoom(Input.GetAxis("Mouse ScrollWheel"));
    }    
    void zoom(float increment){
        Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize - increment,zoomOutMin,zoomOutMax);
    }
}



////////////介紹從手機螢幕控制放大和縮小  6:00開始

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class cameramove : MonoBehaviour
{
    Vector3 touchStart;
    public float zoomOutMin = 1 ;
    public float zoomOutMax = 8 ;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButtonDown(0))
          {touchStart = Camera.main.ScreenToWorldPoint(Input.mousePosition);}
        //以下是放大和縮小的判斷
        if(Input.touchCount == 2)
        {
            Touch touchZero = Input.GetTouch(0);
            Touch touchOne = Input.GetTouch(1);

            Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
            Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;

            float preMagnitude = (touchZeroPrevPos - touchOnePrevPos).magnitude;
            float currentMagnitude = (touchZero.position - touchOne.position).magnitude;

            float difference = currentMagnitude - preMagnitude ;
            zoom(difference*0.01f);
    }   //上面是放大和縮小的判斷
        else if(Input.GetMouseButton(0)) //在本來的if前面加上else,除了兩點碰觸以外執行下列
        {Vector3 direction = touchStart - Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Camera.main.transform.position += direction;}
        zoom(Input.GetAxis("Mouse ScrollWheel"));
    }    
    void zoom(float increment){
        Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize - increment,zoomOutMin,zoomOutMax);
    }
}




Reply