当前位置:网站首页>Unity 鼠标控制第一人称摄像机视角
Unity 鼠标控制第一人称摄像机视角
2022-07-27 14:15:00 【傻子是小傲娇】
第一人称摄像机的实现:
鼠标的移动时的水平距离将决定摄像机在水平方向上的旋转角度,绕着旋转的轴应该为世界坐标系下的y轴
垂直方向上的距离将使摄像机上下旋转,这时候应该绕着自身坐标系下的x轴旋转
为什么不是绕着摄像机的x轴(transform.right),自己改了旋转的轴运行一下就知道了
public class FirstPersonalCamera : MonoBehaviour { public float speed = 5; // Update is called once per frame void Update () { //鼠标在这一帧移动的水平距离 float x = Input.GetAxis("Mouse X"); //绕世界坐标中的y轴旋转 transform.Rotate(Vector3.up * x * speed, Space.World); //鼠标在这一帧移动的垂直距离 float y = Input.GetAxis("Mouse Y"); //绕自身的x轴转 transform.Rotate(Vector3.right * -y * speed); } }
实现鼠标点击时,移动鼠标使得摄像机视角围绕主角旋转
public class CameraView : MonoBehaviour { Transform Player; private float x, y; public float rotateSpeed=5; // Use this for initialization void Awake() { Player = GameObject.FindGameObjectWithTag("Player").transform; //摄像机注视玩家 transform.LookAt(Player); } // Update is called once per frame void Update () { if (Input.GetMouseButton(1)) { //鼠标在这一帧移动的水平距离 x = Input.GetAxis("Mouse X"); //绕着玩家所在的点,世界的y轴旋转 transform.RotateAround(Player.position, Vector3.up, x*rotateSpeed); //鼠标在这一帧移动的垂直距离 y = Input.GetAxis("Mouse Y"); //绕着玩家所在的点,摄像机的x轴旋转 transform.RotateAround(Player.position, transform.right, y * rotateSpeed); } } }
边栏推荐
- Do you really understand CMS garbage collector?
- Dialog manager Chapter 3: create controls
- 网络设备硬核技术内幕 路由器篇 6 汤普金森漫游网络世界(中)
- 【WORK】关于技术架构
- MOS管防止电源反接的原理
- Unity性能优化------DrawCall
- LeetCode 190. 颠倒二进制位 位运算/easy
- Dynamic programming - stock trading 5
- STM32之CAN ---CAN ID过滤器分析
- What is the execution method of the stand-alone parallel query of PostgreSQL?
猜你喜欢

JMeter recording interface automation

LeetCode 190. 颠倒二进制位 位运算/easy

反射

Graphical SQL is too vivid

Finally, someone finished all the dynamic planning, linked list, binary tree and string required for the interview

Dynamic programming - stock trading 5

【ManageEngine】什么是SIEM

LeetCode 74. 搜索二维矩阵 二分/medium

一文搞懂 Redis 架构演化之路
![[ManageEngine] what is Siem](/img/a6/0fbe60df6bef337a91a10fe046aa8a.jpg)
[ManageEngine] what is Siem
随机推荐
数据库使用psql及jdbc进行远程连接,不定时自动断开的解决办法
网络设备硬核技术内幕 路由器篇 20 DPDK (五)
JUC(JMM、Volatile)
网络设备硬核技术内幕 路由器篇 14 从鹿由器到路由器 (中)
对话框管理器第三章:创建控件
光电隔离电路设计方案(六款基于光耦、AD210AN的光电隔离电路图)
多表查询_子查询概述和多表查询_子查询情况1&情况2&情况3
被动收入:回归原始且安全的两种赚取方法
ADB command (install APK package format: ADB install APK address package name on the computer)
TCC
一文搞懂 Redis 架构演化之路
STM32学习之CAN控制器简介
如何帮助企业优化Office管理
NEFU119 组合素数【算术基本定理】
两阶段提交与三阶段提交
【云享读书会第13期】视频文件的封装格式
泛型
[Yunxiang book club issue 13] packaging format of video files
网络设备硬核技术内幕 路由器篇 3 贾宝玉梦游太虚幻境 (中)
网络设备硬核技术内幕 路由器篇 7 汤普金森漫游网络世界(下)
