iLMS知識社群歷程檔Login
Position: 黃國哲 > Unity
1091226 控制3Dcamera旋轉
by 黃國哲 2020-12-26 15:08:13, Reply(0), Views(358)

https://www.itread01.com/content/1549479442.html

手機版本
using UnityEngine;
using System.Collections;

public class cameracoltroller : MonoBehaviour {
    public float sensitivityX = 0.0001f;
    public float sensitivityY = 0.0001f;
void update()
    {
//單指控制攝像頭旋轉
        if(Input.touchCount==1)
        {
            float rotationX = Input.GetAxis("Mouse X") * sensitivityX;
            float rotationY = Input.GetAxis("Mouse Y") * sensitivityY;
            transform.Rotate(-rotationY, rotationX, 0);

        }
//雙指控制攝像頭移動
        if(Input.touchCount==2)
        {
            transform.position += transform.forward * Time.deltaTime * 20;
        }
    }
}


電腦版本
using UnityEngine;
using System.Collections;

public class 旋轉相機2 : MonoBehaviour
{


    public float near = 20.0f;
    public float far = 100.0f;

    public float sensitivetyZ = 2f;
    public float sensitivityX = 10f;
    public float sensitivityY = 10f;
    public float sensitivetyMove = 2f;
    public float sensitivetyMouseWheel = 2f;


    void Update()
    {
        // 滾輪實現鏡頭縮排和拉遠

        //滑鼠右鍵實現視角轉動,類似第一人稱視角
        if (Input.GetMouseButton(1))
        {
            float rotationX = Input.GetAxis("Mouse X") * sensitivityX;
            float rotationY = Input.GetAxis("Mouse Y") * sensitivityY;
            transform.Rotate(-rotationY, rotationX, 0);
        }

        //鍵盤按鈕←和→實現視角水平旋轉
        if (Input.GetAxis("Horizontal") != 0)
        {
            float rotationZ = Input.GetAxis("Horizontal") * sensitivetyZ;
            transform.Rotate(0, 0, rotationZ);
        }
    }
}






Reply