难以绕着父轴旋转子级 [英] Difficulty with rotating child around parent axes

查看:102
本文介绍了难以绕着父轴旋转子级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道关于旋转有100万个问题,我已经尝试并阅读了很多东西,但似乎无法解决这个特定问题.

I know there's a million questions on rotating, I've tried and read many things but I can't seem to solve this particular problem.

我想正确解释的唯一方法是绘制一些不良的图表,所以去吧!

The only way I can think to explain it properly is with some poorly drawn diagrams so here goes!

我有一个父对象和一个子对象(在这里,它表示为一个骰子,相对的面总计为7),此图中的轴为父X,Y和Z方向

I have a parent object, and a child object (here it's represented as a die where opposite faces add up to 7), the axes in this diagram are the parents X, Y, and Z directions

起点:

我希望子对象具有两个方法,分别称为RotateZRotateX. RotateZRotateX应该使子骰子绕父母Z和X轴旋转90度.

I want the child object to have two methods that I'll call RotateZ and RotateX. RotateZ and RotateX should rotate the child dice around the parents Z and X axes in 90 degree steps.

RotateX:

RotateZ:

我当前拥有的代码是这个

The code I currently have is this

void RotateZ(int value) {
    transform.rotation *= Quaternion.AngleAxis(90f * value, pivot.transform.forward);
}

void RotateX(int value) {
    transform.rotation *= Quaternion.AngleAxis(90f * value, pivot.transform.right);
}

我应该注意,pivot是父对象

I should note pivot is the parent object

如果孩子从(0, 0, 0)旋转开始,这似乎很好用,以(0, 0, 0)起点重复调用RotateZRotateX则效果很好.例如,如果我先呼叫RotateX然后呼叫RotateZ,就会出现此问题.

This appears to work great if the child starts at (0, 0, 0) rotation, repeatedly calling RotateZ or RotateX with (0, 0, 0) starting point works great. This problem comes if for example I call RotateX then RotateZ.

在最初的RotateX调用之后,它看起来像这样,使孩子旋转了(90, 0, 0).

After the initial RotateX call it will look like this, giving the child a rotation of (90, 0, 0).

然后在RotateZ之后需要的结果是:

Then after a RotateZ the desired result is this:

但是我最终得到了这个

我对自己做错的事真的很茫然.我有种感觉,我已经达到了用欧拉角可以做的极限,并且我需要学习四元数以防止出现此问题.我需要什么旋转功能才能获得理想的结果?

I'm really at a loss as to what I'm doing wrong. I have a feeling I've hit the limit of what I can do with Euler angles and I need to learn quaternions to prevent this problem. What rotation function do I need to get the desired result?

推荐答案

看起来您需要的是真正很棒的电话

It looks like what you need is the really awesome call

它是Unity中最重要的之一. RotateAround doco

it's one of the most important in Unity. RotateAround doco

别忘了RotateAround围绕一个向量-任何向量.

Don't forget RotateAround rotates around a vector - ANY vector.

视频游戏工程中的一项普遍技术是:您使用标记" -表示只是一个空的游戏对象-您将其附加到对象上或将其附加到对象上.

A ubiquitous technique in video game engineering is: you use markers" - meaning just an empty game object - which you attach to objects or to which you attach your objects.

这是在代码中执行此操作的方法.将标记设为"controlRotator" ...

Here's how to do it in code. Make a marker "controlRotator"...

  1. 使controlRotator 您的controlObject的子对象 .

使其局部完全笔直:即,与父对象controlObject完全相同,即局部旋转= Identity

make it perfectly straight locally: ie, simply exactly the same orientation as the parent, controlObject, ie, local rotation = identity

接下来,巧妙地 将controlObject的WORLD位置设置为smallCube的WORLD位置...

next, cleverly set the WORLD position of controlObject to exactly the WORLD position of smallCube...

您的smallCube,记下它的父项.从那个父母那里取下smallCube.

your smallCube, make a note of it's parent. take smallCube off that parent.

接下来,暂时将smallCube设为controlRotator的子代

...现在您可以...

... and now you can ...

  1. 以任意方式旋转controlRotator!

您只需旋转controlRotator,而不是小立方体.

You simply rotate controlRotator, not the small cube.

请注意,您现在正在完全按照您的要求进行操作,您正在使用controlObject的轴来旋转smallCube .聪明吧?

Note that you are now doing exactly what you asked to do, you're rotating smallCube using the axis of the controlObject. Clever right?

  1. 最后,将smallCube放回到在步骤4中保存的父级.

这就是您的操作方式.实际上,这很常见,您将拥有扩展名或功能来实现.

This is exactly how you do it. In practice this is so commonplace that you'll have an extension or function to do it.

请注意,可以根据需要动态设置GameObject controlRotator,并在扭曲之后将其删除. (这只是数学上的抽象.)

Note that it is fine to make the GameObject controlRotator on the fly as needed, and get rid of it after the twist. (It's only a mathematical abstraction.)

此操作应在打开"状态下进行. controlObject.制作一个名为

This operation should work "on" controlObject. Make a script called

TwistSomethingOnMyAxis.cs

并将其放在CONTROLOBJECT上.

and put that ON the CONTROLOBJECT.

会打个电话

 public void twistThisThing(Transform twistIt) {}

您要做的是,当您想扭曲小立方体时,实际上是在控制对象上调用了TwistSomethingOnMyAxis.你看?就像

What you do is, when you want to twist SMALL CUBE, you actually call to the TwistSomethingOnMyAxis ON THE CONTROL OBJECT. You see? So like

TwistSomethingOnMyAxis:twistor = 
  controlObject.GetComponent<TwistSomethingOnMyAxis>();
twistor.twistThisThing( smallCube , .. 90 degrees on Y or whatever .. );

我希望这是有道理的!享受吧!

I hope that makes sense! Enjoy!

这篇关于难以绕着父轴旋转子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆