您的位置:首页 > 移动开发 > Unity3D

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

Transform.eulerAngles

Switch to Manual
public 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.Variables
current
and
target
are assumed to be in degrees.For optimization reasons, negative values of
maxDelta
are not supported and may cause oscillation. To push
current
away 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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: