保持一个轴的旋转并将其他轴与父轴对齐

Keeping Rotation of one Axis and aligning others to Parent

我在保持一个轴锁定时遇到问题。我有两个工具可以用来抓取这个模拟针状物体。

我只需要能够保持其围绕自身旋转(局部 y)并将其余部分与工具的旋转对齐。然而,目前,我只能弄清楚如何像这样旋转所有轴:

代码如下所示:

 case 1:
 this.transform.rotation = leftParent.transform.rotation;
 this.transform.SetParent (leftParent);
 break;
 case 2:
 this.transform.rotation = rightParent.transform.rotation;
 this.transform.SetParent (rightParent);
 break;

我尝试过的:

如果我遗漏了一些明显的东西,请告诉我。谢谢!

我想你要找的是 Transform.localRotationTranform.localEulerAngles :你可以找到更多关于那些 here.

在你的情况下我会做这样的事情(考虑到你的 "hand" 对象几何是预期的):

case1 :
//Adjust the rotation you want to keep here (I assume on Y axis in your case)
transform.SetParent(leftParent);
transform.localEulerAngles = new Vector3(0.0f, transform.localEulerAngles.y, 0.0f);
break;
case2 :
//Adjust the rotation you want to keep here (I assume on Y axis in your case)
transform.SetParent(rightParent);
transform.localEulerAngles = new Vector3(0.0f, transform.localEulerAngles.y, 0.0f);
break;

顺便说一下,在您的情况下不需要 this 关键字,因为 transform 单独指的是附加脚本的 Transform