当前位置:网站首页>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); //
}
}
}
边栏推荐
- R语言做面板panelvar例子
- CWE4.8 -- The 25 most damaging software security issues in 2022
- [Virtualization Ecological Platform] Platform Architecture Diagram & Ideas and Implementation Details
- VBA实现双击单元格自动输出对号再次双击取消对号
- Life is endless, there are more questions, simple questions to learn knowledge points
- 准确率(Accuracy)、精度(Precision)、召回率(Recall)和 mAP 的图解
- The item 'node.exe' was not recognized as the name of a cmdlet, function, script file, or runnable program.
- CWE4.8 -- 2022年危害最大的25种软件安全问题
- 一文带你了解redux的工作流程——actionreducerstore
- 分布式id解决方案
猜你喜欢

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

3D激光SLAM:LeGO-LOAM论文解读---点云分割部分

三六零与公安部三所发布报告:关基设施保护成为网络安全博弈关键

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

一文带你了解redux的工作流程——actionreducerstore

AWS Amazon cloud account registration, free application for 12 months Amazon cloud server detailed tutorial

初始JDBC 编程

Initial JDBC programming

普林斯顿微积分读本03第二章--编程实现函数图像绘制、三角学回顾

After Effects 教程,如何在 After Effects 中修复曝光不足的镜头?
随机推荐
科学论文和学术论文写作
【虚拟化生态平台】平台架构图&思路和实现细节
[Virtualization ecological platform] Raspberry Pi installation virtualization platform operation process
3D激光SLAM:LeGO-LOAM论文解读---完整篇
Threading(in thread main)
线程池 ThreadPoolExecutor 详解
若枚举映射的值不存在,则不进行反序列化
MySQL模糊查询性能优化
If the value of the enum map does not exist, deserialization is not performed
一文吃透接口调用神器RestTemplate
502 bad gateway原因、解决方法
Data Lake (19): SQL API reads Kafka data and writes it to Iceberg table in real time
AWS亚马逊云账号注册,免费申请12个月亚马逊云服务器详细教程
The most complete phpmyadmin vulnerability summary
订song餐系统
Distributed id solution
「MySQL」- 基础增删改查
关于==和equals的区别和联系,面试这么回答就可以
B/S架构模式的一个整体执行流程
MySQL 的几种碎片整理方案总结(解决delete大量数据后空间不释放的问题)