博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unity Game窗口中还原Scene窗口摄像机操作 强化版
阅读量:7245 次
发布时间:2019-06-29

本文共 2754 字,大约阅读时间需要 9 分钟。

之前写的那个版本看来真的是不行啊。最近研究了一下官方第一人称脚本,人家的平滑过渡真的是没得说。借鉴了一下,写出来了一个新的比较完美的控制。

之前我们的操作是通过鼠标输入的开始坐标和转动坐标。其实官方有一个函数~

1 float yRot = Input.GetAxis("Mouse X");2 float xRot = Input.GetAxis("Mouse Y");

这就分别能获取到鼠标的X轴操作和Y轴操作了。

那为什么用yRot获取X轴,xRot获取Y轴呢?

左面是鼠标的顶视图,右边是Unity中的三维坐标。可以观察到,鼠标X轴的平移对应的就是Unity中Y轴的旋转。Y轴同理。

但是还是不能照搬官方的写法,因为官方的写法针对的是自身坐标,就是Local。(注:LocalPosition并不等于物体的Local坐标)

Scene窗口的摄像机是针对World的旋转。

这里就需要转换一下。

 

首先我们先得到摄像机的目前旋转角度,我们在Start初始化一下

1     void Start()2     {3         CameraR = Camera.main.transform.rotation.eulerAngles;4     }

在Update中用Vector3的形式修改旋转

1             //官方脚本2             float yRot = Input.GetAxis("Mouse X");3             float xRot = Input.GetAxis("Mouse Y");4 5             Vector3 R = CameraR + new Vector3(-xRot, yRot, 0f); //加上旋转距离6 7             CameraR = Vector3.Slerp(CameraR, R, 100f * Time.deltaTime);//平滑过渡8 9             transform.rotation = Quaternion.Euler(CameraR);

给出完整脚本

1 using UnityEngine; 2 using System.Collections; 3  4 public class CameraCotrel : MonoBehaviour { 5  6     private float Speed = 100f; 7     private Vector3 CameraR; 8  9     void Start()10     {11         CameraR = Camera.main.transform.rotation.eulerAngles;12     }13     14     void Update ()15     {16         Vector3 Face = transform.rotation * Vector3.forward;17         Face = Face.normalized;18 19         Vector3 Left = transform.rotation * Vector3.left;20         Left = Left.normalized;21 22         Vector3 Right = transform.rotation * Vector3.right;23         Right = Right.normalized;24 25         if (Input.GetMouseButton(1))26         {27             //官方脚本28             float yRot = Input.GetAxis("Mouse X");29             float xRot = Input.GetAxis("Mouse Y");30 31             Vector3 R = CameraR + new Vector3(-xRot, yRot, 0f);32 33             CameraR = Vector3.Slerp(CameraR, R, Speed * Time.deltaTime);34 35             transform.rotation = Quaternion.Euler(CameraR);36         }37 38         if (Input.GetKey("w"))39         {40             transform.position += Face * Speed * Time.deltaTime;41         }42 43         if (Input.GetKey("a"))44         {45             transform.position += Left * Speed * Time.deltaTime;46         }47 48         if (Input.GetKey("d"))49         {50             transform.position += Right * Speed * Time.deltaTime;51         }52 53         if (Input.GetKey("s"))54         {55             transform.position -= Face * Speed * Time.deltaTime;56         }57         58         if (Input.GetKey("q"))59         {60             transform.position -= Vector3.up * Speed * Time.deltaTime;61         }62 63         if (Input.GetKey("e"))64         {65             transform.position += Vector3.up * Speed * Time.deltaTime;66         }67 68     }69 }

 

 

 

转载于:https://www.cnblogs.com/SHOR/p/5736596.html

你可能感兴趣的文章
rpmbuild SPEC文件
查看>>
心在山水间
查看>>
ionic开发android app步骤
查看>>
【数据结构】位图BitMap与布隆过滤器BloomFilter
查看>>
mysql主从 主主
查看>>
Java中FileInputStream和FileOutputStream类实现文件夹及文件的复制粘贴
查看>>
tomcat+jdk部署
查看>>
Toast源码深度分析
查看>>
zabbix监控公网机器
查看>>
python requests模块详解
查看>>
PHP应用架构演化
查看>>
Python定义全局变量的用法
查看>>
RESTful API使用详解
查看>>
linux下php扩展ssh2的详解
查看>>
final关键字(最终的)
查看>>
mySQL (关系型数据库管理系统)
查看>>
Centos7配置Apache实现HTTPS
查看>>
npm的使用
查看>>
2018.12.26|区块链技术头条
查看>>
SharePoint:使用Indexed Column提高SharePoint 大型文档库或列表访问
查看>>