当前位置:网站首页>Unity mouse controls camera drag, rotation and zoom (simulation editor camera function)
Unity mouse controls camera drag, rotation and zoom (simulation editor camera function)
2022-07-23 12:53:00 【Code writing for a long time, eyes uncomfortable】
There are many similar contents on the Internet , But some are not comprehensive , Even incomplete , Less context , This will cause errors after copying the code , I hate this eunuch code !!
Tell the truth , Fake ten thousand books , The full text of the code is as follows : Copy it into your own script , Hang it on the camera to run
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraControl : MonoBehaviour
{
[Tooltip("code auto get")]
[SerializeField]
private Camera mainCamera;
private Transform cameraTrans;
private int xAxisCoefficient = 1;
private int yAsixCoefficient = 1;
private string _mouseXString = "Mouse X";
private string _mouseYString = "Mouse Y";
private string _mouseScrollWheel = "Mouse ScrollWheel";
private Vector3 _currentMouseRotate = Vector3.zero;
public float fieldOfViewMin = 20.0f;
public float fieldOfviewMax = 100.0f;
public float moveSpeed = 3;
public float sensitivityDrag = 1;
public float sensitivityRotate = 3;
public float sensitivetyMouseWheel = 30;
#region The panel needs to restart after modifying this option
public bool xAxisinversion = false;
public bool yAsixinversion = false;
#endregion
private void Start()
{
mainCamera = gameObject.GetComponent<Camera>();
if (mainCamera == null)
{
Debug.LogErrorFormat(" Camera is empty !");
return;
}
cameraTrans = mainCamera.transform;
xAxisCoefficient = xAxisinversion ? -1 : 1;
yAsixCoefficient = yAsixinversion ? -1 : 1;
}
private void Update()
{
if (cameraTrans == null) return;
KeyBoardControl();
MouseLeftDragControl();
MouseRightRotateControl();
MouseScrollwheelScale();
}
/// <summary>
/// The keyboard moves
/// </summary>
private void KeyBoardControl()
{
if (Input.GetKey(KeyCode.W))
{
cameraTrans.Translate(Vector3.forward * Time.deltaTime * moveSpeed);
}
if (Input.GetKey(KeyCode.S))
{
cameraTrans.Translate(Vector3.back * Time.deltaTime * moveSpeed);
}
if (Input.GetKey(KeyCode.A))
{
cameraTrans.Translate(Vector3.left * Time.deltaTime * moveSpeed);
}
if (Input.GetKey(KeyCode.D))
{
cameraTrans.Translate(Vector3.right * Time.deltaTime * moveSpeed);
}
}
/// <summary>
/// Drag the camera with the left mouse button
/// </summary>
private void MouseLeftDragControl()
{
if (Input.GetMouseButton(0))
{
Vector3 p1 = cameraTrans.position - cameraTrans.right * Input.GetAxisRaw(_mouseXString) * sensitivityDrag * Time.timeScale;
Vector3 p2 = p1 - cameraTrans.up * Input.GetAxisRaw(_mouseYString) * sensitivityDrag * Time.timeScale;
cameraTrans.position = p2;
}
}
/// <summary>
/// Right click to rotate
/// </summary>
private void MouseRightRotateControl()
{
if (Input.GetMouseButton(1))
{
_currentMouseRotate.x = (Input.GetAxis(_mouseYString) * sensitivityRotate) * xAxisCoefficient;
_currentMouseRotate.y = (Input.GetAxis(_mouseXString) * sensitivityRotate) * yAsixCoefficient;
cameraTrans.rotation = Quaternion.Euler(cameraTrans.eulerAngles + _currentMouseRotate);
}
}
/// <summary>
/// Mouse Wheel Zoom
/// </summary>
private void MouseScrollwheelScale()
{
if (Input.GetAxis(_mouseScrollWheel) == 0) return;
mainCamera.fieldOfView = mainCamera.fieldOfView - Input.GetAxis(_mouseScrollWheel) * sensitivetyMouseWheel;
mainCamera.fieldOfView = Mathf.Clamp(mainCamera.fieldOfView, fieldOfViewMin, fieldOfviewMax);
}
}
If it helps you , With pleasure !
边栏推荐
- 记一次日志文件IO系统的设计心得
- C custom set
- Explanation of websocket protocol
- Homework of the fifth week
- [database] basic theory
- HCIP---OSPF细节讲解
- OSPF comprehensive experiment in hcip-mgre environment
- Analyze redis cluster
- C# 自定义栈(Stack)
- Gameframework: package resources, publish packages with the app, package and generate folder instructions, upload resources to the server, download resources, gamefreamworklist DAT and gameframeworkve
猜你喜欢
随机推荐
FTP实验及概述
Instant messaging websocket
详解各种网络协议
Delete node in binary sort tree
[memory understand the difference and function between ram flash and EEPROM memory]
Hcip --- OSPF details
学习日记——(路由与交换技术)三层交换机
Unity3d:UGUI,UI与特效粒子层级,2018.2以上版本BakeMesh,粒子在两个Image之间且在ScrollView
GameFramework:Resource加载,资源加载,依赖加载,任务池,对象池,引用计数
Unity3d+GameFramework:资源分析,资源依赖,循环依赖检测
OSPF routing strategy and Traffic Capture
C: stack stack source code, array stack, chain stack
GameFramework:资源热更代码分析,检查版本信息,下载版本文件,校验版本文件,得到更新文件数量,下载文件,TaskPool
Explain TCP segmentation and IP fragmentation in detail
[AUTOSAR storage stack NVM]
学习日记——(路由与交换技术)OSPF协议
浅析UDP协议和TCP协议
如何用普通的文本编辑器写Web页面
在二叉排序树中删除节点
Learning diary - (routing and switching technology) ACL access control list









