unity3d中的欧拉角
2016-01-12 18:15
302 查看
1.欧拉角Vector3(x,y,z)代表的是旋转物体(若是标准旋转那么是旋转坐标轴x,y,z,转换为旋转物体则旋转角度取反顺序不变),且是将物体从物体坐标系旋转到惯性坐标系(世界坐标系中为了渲染),故旋转顺序为 z, y, x也就是roll pitch yaw。2.欧拉角有别名和万向锁问题,不要随便增长欧拉角的值,也不要单独改变欧拉角的一个旋转角度值,而是用一个Vector3全部一起改变。3.欧拉角来自于Transform.eulerAngles, 渲染时候也会转换到Transform.rotation四元数然后到旋转矩阵来旋转物体。4.欧拉角插值使用 float fAngles = Mathf.MoveTowardsAngle(oldAngles, target, m_rotSpeed * Time.deltaTime); m_transform.eulerAngles = new Vector3(0, fAngles, 0);
测试实例:[csharp] view plaincopyprint?
// align Camera
float fRoll = 0.0f; // 不能绕Z轴滚动
// 欧拉角中的绕X轴旋转,对应Mouse Y移动(上下),
float rPitch = Input.GetAxis("Mouse Y");
// 欧拉角中的绕Y轴旋转,对应Mouse X移动(左右)
float rYaw = Input.GetAxis("Mouse X");
// 鼠标的上下左右移动,转换为主角的旋转欧拉角
// eulerAngles是(yaw, pitch,roll)
// rPitch取负数,是因为Mouse Y向上移动希望是往前看绕X轴旋转反向,像Mouse Y向下是增大绕Y轴旋转正向
Vector3 vecMouse = new Vector3(-rPitch, rYaw, fRoll);
m_camRotateAngles += vecMouse; // m_camRotateAngles是摄像机旋转欧拉角
// 整个摄像机旋转,主角的旋转影响到摄像机旋转(包括上下左右),欧拉角需要一次赋值
m_camTransform.eulerAngles = m_camRotateAngles;
//// 原来的设计,也是正确的
//float rh = Input.GetAxis("Mouse X");
//float rv = Input.GetAxis("Mouse Y");
//m_camRotateAngles.x -= rv;
//m_camRotateAngles.y += rh;
//m_camTransform.eulerAngles = m_camRotateAngles;
// 主角旋转,Player不会绕Z轴旋转,也不会绕x轴旋转,只是会绕Y轴做左右旋转
Vector3 transformRot = m_camTransform.eulerAngles;
transformRot.x = transformRot.z = 0;
m_transform.eulerAngles = transformRot;
下面是官方说明文档:http://docs.unity3d.com/ScriptReference/Transform-eulerAngles.html
public Vector3 eulerAngles;
Only use this variable to read and set the angles to absolute values. Don't increment them, as it will fail when the angle exceeds 360 degrees.Use Transform.Rotate instead.
测试实例:[csharp] view plaincopyprint?
// align Camera
float fRoll = 0.0f; // 不能绕Z轴滚动
// 欧拉角中的绕X轴旋转,对应Mouse Y移动(上下),
float rPitch = Input.GetAxis("Mouse Y");
// 欧拉角中的绕Y轴旋转,对应Mouse X移动(左右)
float rYaw = Input.GetAxis("Mouse X");
// 鼠标的上下左右移动,转换为主角的旋转欧拉角
// eulerAngles是(yaw, pitch,roll)
// rPitch取负数,是因为Mouse Y向上移动希望是往前看绕X轴旋转反向,像Mouse Y向下是增大绕Y轴旋转正向
Vector3 vecMouse = new Vector3(-rPitch, rYaw, fRoll);
m_camRotateAngles += vecMouse; // m_camRotateAngles是摄像机旋转欧拉角
// 整个摄像机旋转,主角的旋转影响到摄像机旋转(包括上下左右),欧拉角需要一次赋值
m_camTransform.eulerAngles = m_camRotateAngles;
//// 原来的设计,也是正确的
//float rh = Input.GetAxis("Mouse X");
//float rv = Input.GetAxis("Mouse Y");
//m_camRotateAngles.x -= rv;
//m_camRotateAngles.y += rh;
//m_camTransform.eulerAngles = m_camRotateAngles;
// 主角旋转,Player不会绕Z轴旋转,也不会绕x轴旋转,只是会绕Y轴做左右旋转
Vector3 transformRot = m_camTransform.eulerAngles;
transformRot.x = transformRot.z = 0;
m_transform.eulerAngles = transformRot;
下面是官方说明文档:http://docs.unity3d.com/ScriptReference/Transform-eulerAngles.html
Transform.eulerAngles
Switch to Manualpublic Vector3 eulerAngles;
Description
The rotation as Euler angles in degrees.The x, y, and z angles represent a rotation z degrees around the z axis, x degrees around the x axis, and y degrees around the y axis (in that order).Only use this variable to read and set the angles to absolute values. Don't increment them, as it will fail when the angle exceeds 360 degrees.Use Transform.Rotate instead.
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { public float yRotation = 5.0F; void Update() { yRotation += Input.GetAxis("Horizontal"); transform.eulerAngles = new Vector3(10, yRotation, 0); } void Example() { print(transform.eulerAngles.x); print(transform.eulerAngles.y); print(transform.eulerAngles.z); } }Do not set one of the eulerAngles axis separately (eg. eulerAngles.x = 10; ) since this will lead to drift and undesired rotations.When setting them to a new value set them all at once as shown above.Unity will convert the angles to and from the rotation stored in Transform.rotation.
Mathf.MoveTowardsAngle
public static float MoveTowardsAngle(float current,float target,float maxDelta);Parameters
Description
Same as MoveTowards but makes sure the values interpolate correctly when they wrap around 360 degrees.Variablescurrentand
targetare assumed to be in degrees.For optimization reasons, negative values of
maxDeltaare not supported and may cause oscillation. To push
currentaway from a target angle, add 180 to that angle instead.
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { public float target = 270.0F; public float speed = 45.0F; void Update() { float angle = Mathf.MoveTowardsAngle(transform.eulerAngles.y, target, speed * Time.deltaTime); transform.eulerAngles = new Vector3(0, angle, 0);
相关文章推荐
- Unity资源管理汇总
- Unity3D中的Coroutine详解
- unity3d热更新插件uLua学习整理
- Unity3d 开发(六) 5.x AssetBundle使用
- Unity3D入门
- OpenCV Configuration on Visual Studio 2015 Community
- (转)续上文,Unity3D面试ABC
- Unity3D安卓打包参数配置与兼容性的关系分析
- 解决Animation 添加AnimationClip 无效的问题
- unity3d 技能编辑器
- unity UGUI 事件接口
- 关于unity中的update、Lateupdate和FixedUpdate
- Unity Shader.Find 找不到内置shader
- unity3d远程加载资源模型到本地并加载(一)打包资源
- Unity Shader中顶点坐标的大小似乎会受到Draw Call合批的影响
- Unity3d导出Recast geomset.txt
- Unity3d导出Recast geomset.txt
- 【Unity】关于一个材质球有多个属性
- Unity 3D Quaternion的使用
- 【Unity ImageEffect】一个用于角色遮挡透视轮廓的效果