unity3d把 x的欧拉角赋给y啥作用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity3d把 x的欧拉角赋给y啥作用相关的知识,希望对你有一定的参考价值。

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);
测试实例:
// 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);

参考技术A

欧拉计划第五题

求1,20的最小公倍数,十分简单,

def gcd(x, y):
    if x < y:
        x, y = y, x
    while y:
        x, y = y, x % y
    return x


def lcm(x, y):
    return x * y / gcd(x, y)


ans = 1
for x in range(2,21):
    ans = lcm(ans, x)
print(ans)

以上是关于unity3d把 x的欧拉角赋给y啥作用的主要内容,如果未能解决你的问题,请参考以下文章

VR-四元素、欧拉角转换条件

欧拉角与四元数(计算,编程)

使用 kinect 旋转骨骼(X、Y、Z 轴的欧拉角)

ros Python 四元数转欧拉角

欧拉角的详解

欧拉角四元数和矩阵的对比(转)