【问题标题】:Rotate GameObject on z degrees around the y axis围绕 y 轴在 z 度上旋转 GameObject
【发布时间】:2017-12-23 14:06:35
【问题描述】:

我有一个游戏对象,它围绕 z 轴倾斜 x 度,围绕 x 轴倾斜 z 度。我设法使用以下代码做到了这一点

public float xtilt, ztilt, ytilt;

//These virables are public float values for how much tilt or rotation I want. 
//xtilt for how much rotation when moving on the x axis. 
//ztilt for how much rotation when moving on the z axis. 
//ytilt for how much rotation when moving on the y axis.

void FixedUpdate()
{
  Rigidbody rb;
  rb.rotation = Quaternion.Euler(rb.velocity.z * ztilt, 0.0f, rb.velocity.x * -xtilt);
}

上面的代码给了我关注

  • 当围绕 z 轴移动时,游戏对象旋转 x 度。
  • 当围绕 x 轴移动时,游戏对象旋转 z 度。

目标:

我要添加:围绕 y 轴旋转或倾斜游戏对象 x 度。

所以它会是这样的:

  • 当围绕 y 轴移动时,游戏对象旋转 x 度。

我试图绕过代码来实现这一点,但没有成功。

有什么想法吗??

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    假设ztilt * rb.velocity.z = z degrees

    如果您想使用 zdegrees 围绕 Y 轴旋转对象,那不应该取代 Y 参数吗?像这样:

    rb.rotation = Quaternion.Euler(0f, rb.velocity.z * ztilt, 0f);
    

    在统一文档中,欧拉函数的声明是:

    public static Quaternion Euler(float x, float y, float z); 
    

    【讨论】:

    • 您的第一个代码只会围绕 z 轴旋转 y 度。
    【解决方案2】:

    如果你想要z度,那么使用包含z值的变量:

    替换

    rb.velocity.y * ytilt
    

    通过

    rb.velocity.y * ztilt
    

    【讨论】:

    • 变量 ztilt 和 ytilt 只是我为我想要的倾斜度设置的值。所以它只会用 ztilt 的值倾斜或旋转 y 度
    • 也许我没有正确理解...对我来说,您似乎想围绕 x 旋转 angleX,围绕 y 旋转 angleZ,围绕 Z 旋转 angleZ,对吗?如果是,rb.rotation = Quaternion.Euler(angleX, angleZ, angleZ); 应该这样做。你不是把 y 误认为 z 了吗?
    • 移动X旋转Z度(角度Z),移动Z旋转X度(角度X),移动Y旋转X度(角度X)。那应该发生什么。对我来说,这段代码完全按照我的意愿工作,除了围绕 Y 轴移动游戏对象rb.rotation = Quaternion.Euler(rb.velocity.z * ztilt, 0.0f, rb.velocity.x * -xtilt);
    • 我试图绕过这个代码rb.rotation = Quaternion.Euler(angleX, angleZ, angleZ);,但没有运气。基于将代码描述为这样的文档:“返回围绕 z 轴旋转 z 度,围绕 x 轴旋转 x 度,围绕 y 轴旋转 y 度(按此顺序)的旋转
    • 我觉得你误解了文档。参数应该从你想要的围绕 x 轴的角度开始,然后是 y,然后是 z。您的代码应类似于 Quaternion.Euler(x, y, z)。为什么使用 z 变量作为第一个参数?你用粗体写的那句话意味着 Unity 将应用你从 z 开始的参数,但它仍然必须是第三个参数......你试过绕一个轴旋转吗?你知道绕 x 绕 y 绕 y 绕 y 绕 x 绕不一样吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多