简体   繁体   English

围绕y轴在z度上旋转GameObject

[英]Rotate GameObject on z degrees around the y axis

I have a gameobject that tilts x degrees aound the z axis and z degrees around the x axis. 我有一个游戏对象,该对象围绕z轴倾斜x度,并围绕x轴倾斜z度。 I managed to do that using the following code 我设法使用以下代码做到这一点

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);
}

the code above gives me te following 上面的代码给了我以下内容

  • When moving around the z axis GameObject rotates x degrees. 当绕z轴移动时,GameObject旋转x度。
  • When moving around the x axis GameObject rotates z degrees. 当绕x轴移动时,GameObject旋转z度。

Objectives: 目标:

I want to add : rotating or tilt the gameobject x degrees around the y axis. 我要添加:围绕y轴旋转或倾斜游戏对象x度。

So it will be like this: 因此它将是这样的:

  • When moving around the y axis GameObject rotates x degrees. 当绕y轴移动时,GameObject旋转x度。

I tried to go around the code to achieve this but with no luck. 我试图遍历代码来实现这一目标,但是没有运气。

any ideas?? 有任何想法吗??

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

If you wanted to rotate the object around the Y axis using zdegrees, shouldn't that supplant the Y parameter? 如果您想使用zdegrees绕Y轴旋转对象,那不应该取代Y参数吗? Like so: 像这样:

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

From the unity docs, the declaration for the euler function is: 从unity文档中,euler函数的声明为:

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

If you want z degrees, then use the variable that contains the z value: 如果需要z度,请使用包含z值的变量:

replace 更换

rb.velocity.y * ytilt

by 通过

rb.velocity.y * ztilt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM