当前位置:网站首页>CameraToolUnity中两种摄像机的两种观察控制方式
CameraToolUnity中两种摄像机的两种观察控制方式
2022-07-31 11:43:00 【先生沉默先】
目标环绕
private void SayHello()
{
www.baidu.com
}
自由摄像机
// **********************************************************************
// 文件信息
// 文件名(File Name): FreeCamera.cs
// 作者(Author): WYH
// 创建时间(CreateTime): 2022年7月16日 17:32:05
// Unity版本(UnityVersion): 2021.1.18f1c1
// 脚本描述(Module description):
// **********************************************************************
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
[AddComponentMenu("Camera/FreeCamera")]
public class FreeCamera : MonoBehaviour
{
private Vector3 _oldMousePosition;
private Vector3 _newMousePosition;
[SerializeField]
private float _flMinimumY = 0.2f;
[SerializeField]
private float _flZoomSpeed = 30.0f;
[SerializeField]
private float _flKeyBoardMoveSpeed = 1f;
[SerializeField]
private float _flRoteSpeed = 0.05f;
private float _fldistance = 5;
//private Vector3 centerOffset = Vector3.zero;
private Vector3 _initPos = Vector3.zero;
private Vector3 _initRot = Vector3.zero;
private void Awake()
{
_initPos = transform.position;
_initRot = transform.eulerAngles;
}
private void OnEnable()
{
transform.position = _initPos;
transform.eulerAngles = _initRot;
}
void Update()
{
MoveCameraKeyBoard();
ZoomCamera();
CameraRotation();
_oldMousePosition = Input.mousePosition;
}
/// <summary>
/// 鼠标控制移动
/// </summary>
private void MoveCameraKeyBoard()
{
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
{
transform.Translate(new Vector3(-_flKeyBoardMoveSpeed, 0, 0), Space.Self);
}
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
{
transform.Translate(new Vector3(_flKeyBoardMoveSpeed, 0, 0), Space.Self);
}
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
{
transform.Translate(new Vector3(0, 0, _flKeyBoardMoveSpeed), Space.Self);
}
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
{
transform.Translate(new Vector3(0, 0, -_flKeyBoardMoveSpeed), Space.Self);
}
if (Input.GetKey(KeyCode.E))
{
transform.Translate(new Vector3(0, _flKeyBoardMoveSpeed, 0), Space.World);
}
if (Input.GetKey(KeyCode.Q))
{
if (transform.transform.position.y - _flKeyBoardMoveSpeed >= this._flMinimumY)
transform.Translate(new Vector3(0, -_flKeyBoardMoveSpeed, 0), Space.World);
}
}
/// <summary>
/// 鼠标控制摄像机是视角,上下左右看
/// </summary>
private void CameraRotation()
{
if (Input.GetMouseButton(1))
{
_newMousePosition = Input.mousePosition;
Vector3 dis = _newMousePosition - _oldMousePosition;
float angleX = dis.x * _flRoteSpeed;
float angleY = dis.y * _flRoteSpeed;
transform.Rotate(new Vector3(-angleY, 0, 0), Space.Self);
transform.Rotate(new Vector3(0, angleX, 0), Space.World);
}
}
/// <summary>
/// 摄像机的缩放
/// </summary>
private void ZoomCamera()
{
float offset = Input.GetAxis("Mouse ScrollWheel");
if (offset != 0)
{
offset *= _flZoomSpeed;
_fldistance -= offset;
transform.Translate(Vector3.forward * offset, Space.Self); //
}
}
}
边栏推荐
- 3D激光SLAM:LeGO-LOAM论文解读---完整篇
- Summary of several defragmentation schemes for MySQL (to solve the problem of not releasing space after deleting a large amount of data)
- deeplab实现自己遥感地质分割数据集
- In PLC communication error or timeout or download the prompt solution of the model
- pycharm汉化教程(碧蓝幻想汉化插件安装)
- R 语言data.frame 中的另一行中减去一行
- 便利贴--46{基于移动端长页中分页加载逻辑封装}
- B/S架构模式的一个整体执行流程
- CWE4.8 -- 2022年危害最大的25种软件安全问题
- 初始JDBC 编程
猜你喜欢

最新MySql安装教学,非常详细

Experience innovation and iteration through the development of lucky draw mini-programs

Read through the interface to call the artifact RestTemplate

MySQL row-level locks (row locks, adjacent key locks, gap locks)

数据持久化技术——MP

透过开发抽奖小程序,体会创新与迭代

“带薪划水”偷刷阿里老哥的面经宝典,三次挑战字节,终成正果

PyQt5快速开发与实战 9.5 PyQtGraph在PyQt中的应用 && 9.6 Plotly在PyQt中的应用

Cloudera Manager —— 端到端的企业数据中心管理工具

准确率(Accuracy)、精度(Precision)、召回率(Recall)和 mAP 的图解
随机推荐
关于Mysql数据库的介绍
Redis - Basics
3D激光SLAM:LeGO-LOAM论文解读---点云分割部分
strings包详细文档+示例
Life is endless, there are more questions, simple questions to learn knowledge points
《MySQL高级篇》四、索引的存储结构
After Effects 教程,如何在 After Effects 中修复曝光不足的镜头?
【虚拟化生态平台】平台架构图&思路和实现细节
科学论文和学术论文写作
学习爬虫之Scrapy框架学习(1)---Scrapy框架初学习及豆瓣top250电影信息获取的实战!
ApiPost is really fragrant and powerful, it's time to throw away Postman and Swagger
oracle优化:instr做join条件很慢「建议收藏」
结构化查询语言SQL-关系数据库标准语言
基于C51实现按键控制
[Virtualization ecological platform] Raspberry Pi installation virtualization platform operation process
Use ODBC in Excel to read data from CDS view on SAP BTP platform
分布式事务——分布式事务简介、分布式事务框架 Seata(AT模式、Tcc模式、Tcc Vs AT)、分布式事务—MQ
How to correctly write the binary stream of the file returned by the server to the local file and save it as a file
最全phpmyadmin漏洞汇总
MySQL模糊查询性能优化