0
\$\begingroup\$

In Unity I have an object with a circle collider attached to it and when I apply a torque to this object's rigidbody2D, it rotates as expected. However, I have now attached a sub-object to this object with a polygon (trigger) collider which I am using to detect when something is in a cone in front of the ship. There is no rigid body (2D or 3D) attached to the child object. The only rigid body on the object is the one I am applying torque to.

However, when I do this, the centre of mass of the ship is moved to the centre of this collider, which produces totally the wrong effect. This happens even if I move this collider to the root node (so both it and the circle collider are attached).

I either need a way of getting the rigid body to ignore the child collider/allow me to set the centre of gravity, or else some other way of detecting objects inside a region.

\$\endgroup\$
1
\$\begingroup\$

It's called "compound colliders" and it distributes weight across all colliders. You can fix it in 3D version by simply marking collider as "Trigger".

Unfortunately, changing center of mass in 2D physics is not yet supported.

\$\endgroup\$
  • \$\begingroup\$ Thank you for your clear explanation, I have opened a bug with Unity regarding this issue. For a work around, would you think it would be better to either use a 3D collider in addition to the 2D ones, or a separate game object which I manually "attach" by moving each frame? \$\endgroup\$ – T. Kiley Jan 30 '14 at 18:10
  • \$\begingroup\$ 3D and 2D colliders use completely different functions so it shouldn't be a problem to mix them up. \$\endgroup\$ – Slasher Jan 30 '14 at 21:31
  • \$\begingroup\$ I would be inclined to agree with you, however, since asking this question I have discovered that Unity2D is massively buggy, particularly when it comes to triggers, so I am going to risk it with 3D colliders for triggers and 2D rigid bodies for the actual movement. \$\endgroup\$ – T. Kiley Jan 30 '14 at 21:45
  • \$\begingroup\$ 2D colliders will not detect 3D colliders and vice versa. If you separate game logic for them to do separate things then there is no problem. However if you try to detect a 2D object using a 3D collider with rigidbody physics - it won't work. I guess another way around it is to use Physics2D.OverlapCircle or thers. \$\endgroup\$ – Slasher Jan 30 '14 at 22:22
0
\$\begingroup\$

As Slasher pointer out in his answer, the problem is that the trigger colliders are being included in the computation of the compound collider when in 2D. In 3D (i.e. with 3D colliders) this does not happen and only non-trigger colliders are used.

I have logged this as a bug with Unity, which they have confirmed and apparently fixed locally. They did not provide a date as to when a fix would go live. You can follow the bug report here.

In the mean time, there are two work arounds:

Manual childing

Here you create your trigger collider in a seperate gameobject and "attach" it by setting its transform.position and transform.rotation to be the same each frame.

This is kind of ugly if you have lots of items in your scene as you can't select the object and see the trigger volume. But it does work.

3D colliders

Depending on what you are trying to achieve, you may be able to use 3D colliders as well. For me, I just wanted to know when another object was in a cone in front of the ship. So by attaching sphere colliders and mesh colliders to each object, I could get the result I needed.

Remember, 2D colliders will not register in 3D colliders (and vice versa) so you will need to use both. Also, you can't have 2D/3D colliders on the same object, so the 3D set will need to be a child. Further, they need to have their rigid body set to kinematic so as to not interfere.

This is a good solution as it'll keep everything the same and when the fix arrives you should just be able to swap the colliders out. However, I don't know if there is a large overhead to running both 2D and 3D physics or whether they are essentially simulating 2D as 3D with infinite z-length on each object.

\$\endgroup\$
-1
\$\begingroup\$

Just attach the lightweight rigid body with a collider you need via a hinge joint. That should not affect center of mass as you still have separate rigid bodies.

Then wait until Unity implements com change.

\$\endgroup\$
  • \$\begingroup\$ I've not used hinge joints before, is there a reason I would use these over say, simply setting the position and rotation every frame? \$\endgroup\$ – T. Kiley Jan 30 '14 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.