简体   繁体   English

在Unity中旋转带有铰接子项的2D GameObject时的怪异行为

[英]Weird behavior when rotating 2D GameObject with hinged children in Unity

For more than two days now I am struggling with this seemingly impossible problem: I have a composite GameObject (one parent, 10 children), each one hinged to at least another with angle limits and player-controlled motors. 现在,我花了超过两天的时间来解决这个看似不可能的问题:我有一个复合的GameObject(一个父母,十个孩子),每个人至少有一个角度限制和玩家控制的马达铰接在一起。 Moves perfectly as I wish. 如我所愿完美地移动。 Now I want to be able to flip it. 现在,我希望能够翻转它。 After a while and many trials,rotation seems the best way in order to keep the angles (inverting localSpace does not respect them): 经过一段时间的尝试,旋转似乎是保持角度的最佳方法(反转localSpace不尊重它们):

Vector3 rotPoint = new Vector3 (ParentGameObject.transform.position.x, myY, myZ);
ParentGameObject.transform.rotateAround (rotPoint, Vector3.up, 180.0f);

BUT, if the parent rotates as asked (180 degrees around its Y axis), every child flips but not around the same axis, rather around their own rotation center. 但是,如果父项按要求旋转(围绕其Y轴旋转180度),则每个子项都会翻转,但不会围绕同一轴翻转,而是围绕自己的旋转中心旋转。 This gives ridiculous results as hinges try to get back to the desired position. 当铰链试图回到所需位置时,这给出了荒谬的结果。 How could I fix this? 我该如何解决? Many many thanks in advance... 在此先谢谢了...

In the end I figured out the problem. 最后我找出了问题所在。 My problem had no valuable solution. 我的问题没有有价值的解决方案。

We can rotate the transform around the Y axis even in 2D, but NOT the rigidbody2D, which in the same object will invariably cause serious problems. 即使在2D模式中,我们也可以绕Y轴旋转变换,但对于刚体2D,则不能旋转,这在同一对象中总是会引起严重问题。

This is maybe one remaining inconsistency into Unity5's implementation of 2D. 这可能是Unity5的2D实施中仍然存在的不一致之处。 I had to flip using localScale inversion and programmatically updated positions and rotations. 我不得不使用localScale反转和以编程方式更新的位置和旋转进行翻转。

It sounds like the child GameObjects are rotating around their own pivots instead of the parents. 听起来像子GameObjects绕着自己的轴旋转,而不是围绕父轴旋转。 I haven't got Unity installed at the moment to test this, but if you add an empty GameObject as a child of your parent (and with the same pivot point), then make all your other GameObjects a child of this, it should work. 我目前尚未安装Unity进行测试,但是如果您添加一个空的GameObject作为父级的子级(并具有相同的枢轴点),然后将所有其他GameObjects都作为此级的子级,则应该可以。

You should not move or rotate a rigidbody if you don't have to. 不必移动或旋转刚体。 That will mess up the underlying physics calculations and physics engines won't be happy about it. 这会弄乱基础的物理计算,而物理引擎对此不会感到满意。 Instead, you have other ways to interact with them 相反,您还有其他与他们互动的方式

  • Apply force, torque 施加力,扭矩
  • Apply acceleration, angular acceleration 应用加速度,角加速度
  • Change speed, angular speed 改变速度,角速度

If you have to move/rotate the object, you should also update positions, rotations, speeds, angular speeds of all of your hinged objects. 如果必须移动/旋转对象,还应该更新所有铰接对象的位置,旋转,速度,角速度。 That would not be easy. 那不容易。 Give some more details if you unable to solve. 如果无法解决,请提供更多详细信息。

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

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