資源簡介
Unity GameView相機(jī)移動和旋轉(zhuǎn)腳本,支持相機(jī)移動,自轉(zhuǎn),公轉(zhuǎn)
使用方法,需要移動的相機(jī)GameObject上掛上該腳本
W / ↑:向前移動;
S / ↓:向后移動;
A / ←:向左移動;
D / →:向右移動;
Q:向下移動;
E:向上移動;
鼠標(biāo)中鍵滾動:縮放(相機(jī)FOV);
按住鼠標(biāo)左鍵移動:上下左右移動;
按住鼠標(biāo)右鍵移動:相機(jī)自身旋轉(zhuǎn)(俯仰和偏航);
按住ctrl鍵并用鼠標(biāo)左鍵點擊物體:看向該物體,并設(shè)置該物體為公轉(zhuǎn)中心;點擊沒有物體的地方取消選中
按住ctrl鍵并按住鼠標(biāo)右鍵移動:繞公轉(zhuǎn)中心旋轉(zhuǎn)(沒有選中物時默認(rèn)以相機(jī)前3m的虛擬焦點旋轉(zhuǎn));
代碼片段和文件信息
using?System.Collections;
using?System.Collections.Generic;
using?UnityEditor;
using?UnityEngine;
[RequireComponent(typeof(Camera))]
public?class?CameraMovement?:?MonoBehaviour?{
????private?Camera?cam;
????public?float?moveSpeed?=?10.0f;
????public?float?zoomSpeed?=?20.0f;
????public?float?dragSpeed?=?1.0f;
????public?float?resolutionRadius?=?3.0f;
????public?float?resolutionSpeed?=?0.1f;
????public?float?rotationSpeed?=?5.0f;
????public?Transform?selected;
????private?const?string?MouseX?=?“Mouse?X“;
????private?const?string?MouseY?=?“Mouse?Y“;
????//?Use?this?for?initialization
????void?Start?()?{
????????cam?=?GetComponent();
????}
//?Update?is?called?once?per?frame
void?Update?()?{
????????var?e?=?Event.current;
????????var?deltaTime?=?Time.deltaTime;
????????var?camTrans?=?cam.transform;
????????Vector3?translation?=?Vector3.zero;
????????if?(Input.GetKey(KeyCode.W)?||?Input.GetKey(KeyCode.UpArrow))?//?front
????????{
????????????translation?+=?camTrans.forward?*?moveSpeed?*?deltaTime;
????????}
????????else?if?(Input.GetKey(KeyCode.S)?||?Input.GetKey(KeyCode.DownArrow))?//back
????????{
????????????translation?+=?-camTrans.forward?*?moveSpeed?*?deltaTime;
????????}
????????if?(Input.GetKey(KeyCode.A)?||?Input.GetKey(KeyCode.LeftArrow))
????????{
????????????translation?+=?-camTrans.right?*?moveSpeed?*?deltaTime;
????????}
????????else?if?(Input.GetKey(KeyCode.D)?||?Input.GetKey(KeyCode.RightArrow))
????????{
????????????translation?+=?camTrans.right?*?moveSpeed?*?deltaTime;
????????}
????????if?(Input.GetKey(KeyCode.Q))?//?down
????????{
????????????translation?+=?-Vector3.up?*?moveSpeed?*?deltaTime;
????????}
????????else?if?(Input.GetKey(KeyCode.E))?//?up
????????{
????????????translation?+=?Vector3.up?*?moveSpeed?*?deltaTime;
????????}
????????if?(Input.GetMouseButton(0))?//?xy?translate
????????{
????????????var?drag?=?Input.GetAxis(MouseX)?*?dragSpeed;
????????????translation?+=?-camTrans.right?*?drag;
????????????drag?=?Input.GetAxis(MouseY)?*?dragSpeed;
????????????translation?+=?-camTrans.up?*?drag;
????????}
????????camTrans.position?=?camTrans.position?+?translation;
????????if?(Input.GetMouseButton(1))?
????????{
????????????if?(Input.GetKey(KeyCode.Left
評論
共有 條評論