Unity Game窗口中还原Scene窗口摄像机操作 强化版
2016-08-12 13:15
561 查看
之前写的那个版本看来真的是不行啊。最近研究了一下官方第一人称脚本,人家的平滑过渡真的是没得说。借鉴了一下,写出来了一个新的比较完美的控制。
之前我们的操作是通过鼠标输入的开始坐标和转动坐标。其实官方有一个函数~
这就分别能获取到鼠标的X轴操作和Y轴操作了。
那为什么用yRot获取X轴,xRot获取Y轴呢?
左面是鼠标的顶视图,右边是Unity中的三维坐标。可以观察到,鼠标X轴的平移对应的就是Unity中Y轴的旋转。Y轴同理。
但是还是不能照搬官方的写法,因为官方的写法针对的是自身坐标,就是Local。(注:LocalPosition并不等于物体的Local坐标)
Scene窗口的摄像机是针对World的旋转。
这里就需要转换一下。
首先我们先得到摄像机的目前旋转角度,我们在Start初始化一下
在Update中用Vector3的形式修改旋转
给出完整脚本
之前我们的操作是通过鼠标输入的开始坐标和转动坐标。其实官方有一个函数~
float yRot = Input.GetAxis("Mouse X"); float xRot = Input.GetAxis("Mouse Y");
这就分别能获取到鼠标的X轴操作和Y轴操作了。
那为什么用yRot获取X轴,xRot获取Y轴呢?
左面是鼠标的顶视图,右边是Unity中的三维坐标。可以观察到,鼠标X轴的平移对应的就是Unity中Y轴的旋转。Y轴同理。
但是还是不能照搬官方的写法,因为官方的写法针对的是自身坐标,就是Local。(注:LocalPosition并不等于物体的Local坐标)
Scene窗口的摄像机是针对World的旋转。
这里就需要转换一下。
首先我们先得到摄像机的目前旋转角度,我们在Start初始化一下
void Start() { CameraR = Camera.main.transform.rotation.eulerAngles; }
在Update中用Vector3的形式修改旋转
//官方脚本
float yRot = Input.GetAxis("Mouse X"); float xRot = Input.GetAxis("Mouse Y");
Vector3 R = CameraR + new Vector3(-xRot, yRot, 0f); //加上旋转距离
CameraR = Vector3.Slerp(CameraR, R, 100f * Time.deltaTime);//平滑过渡
transform.rotation = Quaternion.Euler(CameraR);
给出完整脚本
using UnityEngine;
using System.Collections;
public class CameraCotrel : MonoBehaviour {
private float Speed = 100f;
private Vector3 CameraR;
void Start() { CameraR = Camera.main.transform.rotation.eulerAngles; }
void Update ()
{
Vector3 Face = transform.rotation * Vector3.forward;
Face = Face.normalized;
Vector3 Left = transform.rotation * Vector3.left;
Left = Left.normalized;
Vector3 Right = transform.rotation * Vector3.right;
Right = Right.normalized;
if (Input.GetMouseButton(1))
{
//官方脚本
float yRot = Input.GetAxis("Mouse X"); float xRot = Input.GetAxis("Mouse Y");
Vector3 R = CameraR + new Vector3(-xRot, yRot, 0f);
CameraR = Vector3.Slerp(CameraR, R, Speed * Time.deltaTime);
transform.rotation = Quaternion.Euler(CameraR);
}
if (Input.GetKey("w"))
{
transform.position += Face * Speed * Time.deltaTime;
}
if (Input.GetKey("a"))
{
transform.position += Left * Speed * Time.deltaTime;
}
if (Input.GetKey("d"))
{
transform.position += Right * Speed * Time.deltaTime;
}
if (Input.GetKey("s"))
{
transform.position -= Face * Speed * Time.deltaTime;
}
if (Input.GetKey("q"))
{
transform.position -= Vector3.up * Speed * Time.deltaTime;
}
if (Input.GetKey("e"))
{
transform.position += Vector3.up * Speed * Time.deltaTime;
}
}
}
相关文章推荐
- Unity Game窗口中还原Scene窗口摄像机操作
- Unity在Scene窗口有显示,在Game窗口不显示
- unity的2D中物体在Scene窗口中可以看到,但在Game窗口中看不到
- UnityEditor中新建一个窗口,在Hierarchy中点击一个GameObject时,窗口中就显示相应的GameObject名称和Position
- unity3d中的物体,在Scene窗口中可以看到,而在Game窗口中看不到的原因
- Unity 2017 Scene界面与Game界面显示不同/过于灰暗的解决办法
- Unity之编辑器类将摄像机视图(targetTexture)绘制在窗口中
- Unity入门学习 //03_Game窗口
- Unity入门学习 //02_Scene窗口
- 关于Unity任何版本点击Play运行就黑屏,除了摄像机窗口其他全部黑掉的问题解决~
- Tips7:Unity中 Scene视图 和 Game视图 中 视角(Camera)的控制
- [Unity]将摄像机移动到Scene视角位置
- unity3d中的物体,在Scene窗口中可以看到,而在Game窗口中看不到
- 【Unity】game窗口调试Debug
- 猎豹MFC--窗口操作--最大化 最小化还原 关闭 大小位置 居中显示 图标 标题
- Unity 之Bug - Scene视图的物体Game视图不显示
- Unity中摄像机绕物体旋转和拉近拉远视角的操作
- unity窗口最大化最小化及窗口还原
- Unity 小技巧:Scene视图 和 Game视图 中 视角(Camera)的控制
- UNITY在SCENE窗口获取鼠标点击事件