当前位置:网站首页>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 !
边栏推荐
- Learning diary - (routing and switching technology) OSPF Protocol
- 学习日记——(路由与交换技术)OSPF协议
- 学习日记——(路由与交换技术)动态路由(rip协议)和静态路由
- Openssl自行签证流程概述
- HCIP---GRE协议和MGRE环境,以及OSPF协议的相关知识点
- PDF在线预览,pdf.js的使用
- 学习日记(路由与交换技术)——浮动静态路由和缺省路由
- C #: quick sort. If there is the same number, it will be ignored, and then continue the previous search direction to find the next number that meets the requirements for replacement
- Unity3d:ugui source code eventsystem input system FAQ
- PDF Online preview, use of pdf.js
猜你喜欢

Explanation of websocket protocol

Hcip--- BGP related configuration (Federal chapter)

【读书笔记《凤凰架构》- 构架可靠的大型分布式系统.周志明】(一)

学习日记——(路由与交换技术)动态路由(rip协议)和静态路由

Gameframework: package resources, publish packages with the app, package and generate folder instructions, upload resources to the server, download resources, gamefreamworklist DAT and gameframeworkve

Learning diary - (routing and switching technology) ACL access control list

Learning diary - (routing and switching technology) DHCP (Dynamic Host Configuration Protocol)

HCIP---BGP相关配置

Routing and interface technology -- Summary of direct network

WebSocket 协议讲解
随机推荐
Post form submission data limit
Hcip --- mGRE comprehensive experiment
MySQL性能优化,索引优化
Explain various network protocols in detail
路由与交换技术——静态路由
Learning diary - (routing and switching technology) OSPF Protocol
C# 自定义栈(Stack)
[AUTOSAR storage stack NVM]
浅析互联网协议(一)
OSPF和RIP的路由扩展配置
C (CSharp) wechat official account development - basic configuration
Openssl自行签证流程概述
Overview of OpenSSL self visa process
C custom set
Hcip --- HCIA knowledge review (I)
Explain the interactive data flow and block data flow of TCP in detail
详解TCP连接的建立
MySQL performance optimization, index optimization
How many seats do you need for 1000 people to eat in 2 hours?
Analyze redis cluster