欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Unity 鼠标与键盘控制相机移动与旋转

程序员文章站 2024-03-16 20:16:04
...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


 enum MouseButtonDown
	{
		MBD_LEFT = 0,
		MBD_RIGHT,
		MBD_MIDDLE,
	};

	public class HYQCameraController : MonoBehaviour
	{
		// [SerializeField]
		private Vector3 focus = Vector3.zero;
		// [SerializeField]
		private GameObject focusObj = null;

		public bool showInstWindow = true;

		private Vector3 oldPos;

		[Tooltip("缩放系数")]
		[SerializeField] private float ScrollSpeed = 0.9f;
		[Tooltip("移动速度")]
		[SerializeField] private float MoveSpeed = 0.7f;
		[Tooltip("相机旋转速度")]
		[SerializeField] private float RotaSpeed = 0.25f;

		private float KeyRotaCameraSpeed=40f;

		private float inputH,inputV;
		private bool CanMove = true;
		void setupFocusObject(string name)
		{
			GameObject obj = this.focusObj = new GameObject(name);
			obj.transform.position = this.focus;
			obj.transform.LookAt(this.transform.position);

			return;
		}

		void Start ()
		{
			if (this.focusObj == null)
				this.setupFocusObject("CameraFocusObject");

			Transform trans = this.transform;
			transform.parent = this.focusObj.transform;

			trans.LookAt(this.focus);
			return;
		}
	
		void Update ()
		{
			this.mouseEvent();


			

			this.KeyBoardController();
			
			if(Input.GetKey("escape")){
				Application.Quit();
			}
			
			return;
		}
# region Mouse Controller
		void mouseEvent()
		{
			float delta = Input.GetAxis("Mouse ScrollWheel") * ScrollSpeed;
			if (delta != 0.0f)
				this.mouseWheelEvent(-delta);

			if (Input.GetMouseButtonDown((int)MouseButtonDown.MBD_LEFT) ||
				Input.GetMouseButtonDown((int)MouseButtonDown.MBD_MIDDLE) ||
				Input.GetMouseButtonDown((int)MouseButtonDown.MBD_RIGHT))
				this.oldPos = Input.mousePosition;

			this.mouseDragEvent(Input.mousePosition);

			return;
		}

		void mouseDragEvent(Vector3 mousePos)
		{
			//当按下了左,中,右键时先记录按下那一刻鼠标的位置,储存为oldPos,然后执行拖动鼠标移动相机的操作
			Vector3 diff = mousePos - oldPos;

			if(Input.GetMouseButton((int)MouseButtonDown.MBD_LEFT))
			{
				//Operation for Mac : "Left Alt + Left Command + LMB Drag" is Track
				if(Input.GetKey(KeyCode.LeftAlt) && Input.GetKey(KeyCode.LeftCommand))
				{
					if (diff.magnitude > Vector3.kEpsilon)
						this.cameraTranslate(-diff / 100.0f);
				}
				//Operation for Mac : "Left Alt + LMB Drag" is Tumble
				else if (Input.GetKey(KeyCode.LeftAlt))
				{
					if (diff.magnitude > Vector3.kEpsilon)
						this.cameraRotate(new Vector3(diff.y, diff.x, 0.0f) );
				}
				//Only "LMB Drag" is no action.
			}
			//Track
			else if (Input.GetMouseButton((int)MouseButtonDown.MBD_MIDDLE))
			{
				if (diff.magnitude > Vector3.kEpsilon)
					this.cameraTranslate(-diff / 100.0f);
			}
			//Tumble
			else if (Input.GetMouseButton((int)MouseButtonDown.MBD_RIGHT))
			{
				if (diff.magnitude > Vector3.kEpsilon)
					this.cameraRotate(new Vector3(diff.y, diff.x, 0.0f));
			}
				
			this.oldPos = mousePos;	

			return;
		}

		//Dolly
		public void mouseWheelEvent(float delta)
		{
			Vector3 focusToPosition = this.transform.position - this.focus;

			Vector3 post = focusToPosition * (1.0f + delta);

			if (post.magnitude > 0.01)
				this.transform.position = this.focus + post;

			return;
		}

		void cameraTranslate(Vector3 vec)
		{
			Transform focusTrans = this.focusObj.transform;

			vec.x *= -1;

			focusTrans.Translate(Vector3.right * vec.x * MoveSpeed );
			focusTrans.Translate(Vector3.up * vec.y * MoveSpeed );
			//如果没有按下shift键则开启前台移动,否则关闭前台移动
			if (!(Input.GetKey(KeyCode.LeftShift)|| Input.GetKey(KeyCode.RightShift))){
				focusTrans.Translate(Vector3.forward * vec.z *MoveSpeed);
			}
			

			this.focus = focusTrans.position;

			return;
		}

		public void cameraRotate(Vector3 eulerAngle)
		{
			//Use Quaternion to prevent rotation flips on XY plane
			Quaternion q = Quaternion.identity;
 
			Transform focusTrans = this.focusObj.transform;
			focusTrans.localEulerAngles = focusTrans.localEulerAngles + (eulerAngle *  RotaSpeed);

			//Change this.transform.LookAt(this.focus) to q.SetLookRotation(this.focus)
			q.SetLookRotation (this.focus ) ;

			return;
		}

		#endregion



# region KeyCode Controller
void KeyBoardController(){

	inputH = Input.GetAxis("Horizontal") * Time.deltaTime *MoveSpeed;
	inputV = Input.GetAxis("Vertical") * Time.deltaTime *  MoveSpeed;
	IsCanMove();	//检测是否可以移动
	if (CanMove){
		//左右平移
		if(inputH != 0){
			// transform.Translate(new Vector3(inputH,0,0),Space.Self);
			this.cameraTranslate(new Vector3(inputH,0,0));
		}

		//前后平移
		if(inputV != 0 ){
			// transform.Translate(new Vector3(0,0,inputV),Space.Self);
			this.cameraTranslate(new Vector3(0,0,-inputV));
		}
		//上下平移
		if( Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift) && inputV != 0 ){
			// transform.Translate(new Vector3(0,inputV,0),Space.Self);
			this.cameraTranslate(new Vector3(0,inputV,0));
		}

	}
	

	//旋转相机

	//如果按住Ctrl +左右键,则相机左右旋转

	if((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && inputH != 0){

		
		this.cameraRotate(new Vector3(inputV,inputH,0) * KeyRotaCameraSpeed);

	}

	if((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && inputV != 0){
			
			this.cameraRotate(new Vector3(inputV,inputH,0) * KeyRotaCameraSpeed);

	}
}

void IsCanMove(){
	if(Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)){
		CanMove = false;
	}else{
		CanMove = true;
	}
}

#endregion
	}
相关标签: Unity