Skip to main content
fix typos
Source Link
Kevin
  • 7k
  • 1
  • 12
  • 32

The advice you've found is a little oversimplified. There's an unstated assumption here:

If you have a moving object with a Collder2DCollider2D and you want that movement to interact with other physics objects, then it should have a Rigidbody2D.

Very often that bolded part is implicit in the context. If someone is asking about moving a character in a platformer, or a vehicle in a driving / spaceflight game, then they almost certainly want the Collider2D to...

  • Stop the object from moving into / through solid obstacles.

  • Fire trigger & collision events when it hits enemies / hazards / power-ups.

  • Push other movable objects around.

These things can fail to happen, or happen in glitchy, inconsistent ways when you don't have a rigid body.

And so, you'll see a lot of advice that jumps directly to "always use a rigid body to move" because that's almost always the right answer, and they don't always clarify that it's based on these assumptions.

Your case isn't that though - you only need the collider for mouse picking / raycasting, and don't want any physics interactions with the other physics objects. So, your situation is the rare exception to the rule, where it's actually safe to skip the Rigidbody2D for this purpose.

Performance

Moving a Collier2DCollider2D isn't free. Each time you change the transform, the engine needs to clone those changes into the Box2D simulation so that your raycasts and mouse picks work correctly.

In my tests, that sync-up adds something like .1-.2 ms to the frame time, but not per object. Most of the cost is doing the sync at all, and the per-object cost is much lower, so having a bunch of moving colliders doesn't compound the cost too badly.

In older versions of Unity, moving a collider without a rigid body was much more expensive, and so you'll see a lot of superstition and misinformation about it lingering around the net, saying that skipping the body will tank your performance. (I was guilty of this myself, until a user here pointed out to me that this issue was patched years ago).

The best way to see how the performance compares for your situation/version is to use the built-in profiler to measure it.

When I do that in a recent Unity 6 version, I find adding a Rigidbody2D to a moving Collider2D is still significantly more expensive when it's moving near other colliders than when I do the same test with no body. So you don't gain performance this way - at least not on desktop. To be sure, verify this on your own target platforms with a scene representative of your in-game use case.

To minimize any spurious collision-handling work in these tests, I had "Is Trigger" checked on all my colliders, and I put them all in a "No Collide" physics layer with its interactions disabled with all layers (including itself):

No-Collide layer collision matrix configuration in Project Settings > Physics 2D

The advice you've found is a little oversimplified. There's an unstated assumption here:

If you have a moving object with a Collder2D and you want that movement to interact with other physics objects, then it should have a Rigidbody2D.

Very often that bolded part is implicit in the context. If someone is asking about moving a character in a platformer, or a vehicle in a driving / spaceflight game, then they almost certainly want the Collider2D to...

  • Stop the object from moving into / through solid obstacles.

  • Fire trigger & collision events when it hits enemies / hazards / power-ups.

  • Push other movable objects around.

These things can fail to happen, or happen in glitchy, inconsistent ways when you don't have a rigid body.

And so, you'll see a lot of advice that jumps directly to "always use a rigid body to move" because that's almost always the right answer, and they don't always clarify that it's based on these assumptions.

Your case isn't that though - you only need the collider for mouse picking / raycasting, and don't want any physics interactions with the other physics objects. So, your situation is the rare exception to the rule, where it's actually safe to skip the Rigidbody2D for this purpose.

Performance

Moving a Collier2D isn't free. Each time you change the transform, the engine needs to clone those changes into the Box2D simulation so that your raycasts and mouse picks work correctly.

In my tests, that sync-up adds something like .1-.2 ms to the frame time, but not per object. Most of the cost is doing the sync at all, and the per-object cost is much lower, so having a bunch of moving colliders doesn't compound the cost too badly.

In older versions of Unity, moving a collider without a rigid body was much more expensive, and so you'll see a lot of superstition and misinformation about it lingering around the net, saying that skipping the body will tank your performance. (I was guilty of this myself, until a user here pointed out to me that this issue was patched years ago)

The best way to see how the performance compares for your situation/version is to use the built-in profiler to measure it.

When I do that in a recent Unity 6 version, I find adding a Rigidbody2D to a moving Collider2D is still significantly more expensive when it's moving near other colliders than when I do the same test with no body. So you don't gain performance this way - at least not on desktop. To be sure, verify this on your own target platforms with a scene representative of your in-game use case.

To minimize any spurious collision-handling work in these tests, I had "Is Trigger" checked on all my colliders, and I put them all in a "No Collide" physics layer with its interactions disabled with all layers (including itself):

No-Collide layer collision matrix configuration in Project Settings > Physics 2D

The advice you've found is a little oversimplified. There's an unstated assumption here:

If you have a moving object with a Collider2D and you want that movement to interact with other physics objects, then it should have a Rigidbody2D.

Very often that bolded part is implicit in the context. If someone is asking about moving a character in a platformer, or a vehicle in a driving / spaceflight game, then they almost certainly want the Collider2D to...

  • Stop the object from moving into / through solid obstacles.

  • Fire trigger & collision events when it hits enemies / hazards / power-ups.

  • Push other movable objects around.

These things can fail to happen, or happen in glitchy, inconsistent ways when you don't have a rigid body.

And so, you'll see a lot of advice that jumps directly to "always use a rigid body to move" because that's almost always the right answer, and they don't always clarify that it's based on these assumptions.

Your case isn't that though - you only need the collider for mouse picking / raycasting, and don't want any physics interactions with the other physics objects. So, your situation is the rare exception to the rule, where it's actually safe to skip the Rigidbody2D for this purpose.

Performance

Moving a Collider2D isn't free. Each time you change the transform, the engine needs to clone those changes into the Box2D simulation so that your raycasts and mouse picks work correctly.

In my tests, that sync-up adds something like .1-.2 ms to the frame time, but not per object. Most of the cost is doing the sync at all, and the per-object cost is much lower, so having a bunch of moving colliders doesn't compound the cost too badly.

In older versions of Unity, moving a collider without a rigid body was much more expensive, and so you'll see a lot of superstition and misinformation about it lingering around the net, saying that skipping the body will tank your performance. (I was guilty of this myself, until a user here pointed out to me that this issue was patched years ago).

The best way to see how the performance compares for your situation/version is to use the built-in profiler to measure it.

When I do that in a recent Unity 6 version, I find adding a Rigidbody2D to a moving Collider2D is still significantly more expensive when it's moving near other colliders than when I do the same test with no body. So you don't gain performance this way - at least not on desktop. To be sure, verify this on your own target platforms with a scene representative of your in-game use case.

To minimize any spurious collision-handling work in these tests, I had "Is Trigger" checked on all my colliders, and I put them all in a "No Collide" physics layer with its interactions disabled with all layers (including itself):

No-Collide layer collision matrix configuration in Project Settings > Physics 2D

Adding extra use case for pushing
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

The advice you've found is a little oversimplified. There's an unstated assumption here:

If you have a moving object with a Collder2D and you want that movement to interact with other physics objects, then it should have a Rigidbody2D.

Very often that bolded part is implicit in the context. If someone is asking about moving a character in a platformer, or a vehicle in a driving / spaceflight game, then they almost certainly want the Collider2D to...

  • Stop the object from moving into / through solid obstacles.

  • Fire trigger & collision events when it hits enemies / hazards / power-ups.

  • Push other movable objects around.

These things can fail to happen, or happen in glitchy, inconsistent ways when you don't have a rigid body.

And so, you'll see a lot of advice that jumps directly to "always use a rigid body to move" because that's almost always the right answer, and they don't always clarify that it's based on these assumptions.

Your case isn't that though - you only need the collider for mouse picking / raycasting, and don't want any physics interactions with the other physics objects. So, your situation is the rare exception to the rule, where it's actually safe to skip the Rigidbody2D for this purpose.

Performance

Moving a Collier2D isn't free. Each time you change the transform, the engine needs to clone those changes into the Box2D simulation so that your raycasts and mouse picks work correctly.

In my tests, that sync-up adds something like .1-.2 ms to the frame time, but not per object. Most of the cost is doing the sync at all, and the per-object cost is much lower, so having a bunch of moving colliders doesn't compound the cost too badly.

In older versions of Unity, moving a collider without a rigid body was much more expensive, and so you'll see a lot of superstition and misinformation about it lingering around the net, saying that skipping the body will tank your performance. (I was guilty of this myself, until a user here pointed out to me that this issue was patched years ago)

The best way to see how the performance compares for your situation/version is to use the built-in profiler to measure it.

When I do that in a recent Unity 6 version, I find adding a Rigidbody2D to a moving Collider2D is still significantly more expensive when it's moving near other colliders than when I do the same test with no body. So you don't gain performance this way - at least not on desktop. To be sure, verify this on your own target platforms with a scene representative of your in-game use case.

To minimize any spurious collision-handling work in these tests, I had "Is Trigger" checked on all my colliders, and I put them all in a "No Collide" physics layer with its interactions disabled with all layers (including itself):

No-Collide layer collision matrix configuration in Project Settings > Physics 2D

The advice you've found is a little oversimplified. There's an unstated assumption here:

If you have a moving object with a Collder2D and you want that movement to interact with other physics objects, then it should have a Rigidbody2D.

Very often that bolded part is implicit in the context. If someone is asking about moving a character in a platformer, or a vehicle in a driving / spaceflight game, then they almost certainly want the Collider2D to...

  • Stop the object from moving into / through solid obstacles.

  • Fire trigger & collision events when it hits enemies / hazards / power-ups.

These things can fail to happen, or happen in glitchy, inconsistent ways when you don't have a rigid body.

And so, you'll see a lot of advice that jumps directly to "always use a rigid body to move" because that's almost always the right answer, and they don't always clarify that it's based on these assumptions.

Your case isn't that though - you only need the collider for mouse picking / raycasting, and don't want any physics interactions with the other physics objects. So, your situation is the rare exception to the rule, where it's actually safe to skip the Rigidbody2D for this purpose.

Performance

Moving a Collier2D isn't free. Each time you change the transform, the engine needs to clone those changes into the Box2D simulation so that your raycasts and mouse picks work correctly.

In my tests, that sync-up adds something like .1-.2 ms to the frame time, but not per object. Most of the cost is doing the sync at all, and the per-object cost is much lower, so having a bunch of moving colliders doesn't compound the cost too badly.

In older versions of Unity, moving a collider without a rigid body was much more expensive, and so you'll see a lot of superstition and misinformation about it lingering around the net, saying that skipping the body will tank your performance. (I was guilty of this myself, until a user here pointed out to me that this issue was patched years ago)

The best way to see how the performance compares for your situation/version is to use the built-in profiler to measure it.

When I do that in a recent Unity 6 version, I find adding a Rigidbody2D to a moving Collider2D is still significantly more expensive when it's moving near other colliders than when I do the same test with no body. So you don't gain performance this way - at least not on desktop. To be sure, verify this on your own target platforms with a scene representative of your in-game use case.

To minimize any spurious collision-handling work in these tests, I had "Is Trigger" checked on all my colliders, and I put them all in a "No Collide" physics layer with its interactions disabled with all layers (including itself):

No-Collide layer collision matrix configuration in Project Settings > Physics 2D

The advice you've found is a little oversimplified. There's an unstated assumption here:

If you have a moving object with a Collder2D and you want that movement to interact with other physics objects, then it should have a Rigidbody2D.

Very often that bolded part is implicit in the context. If someone is asking about moving a character in a platformer, or a vehicle in a driving / spaceflight game, then they almost certainly want the Collider2D to...

  • Stop the object from moving into / through solid obstacles.

  • Fire trigger & collision events when it hits enemies / hazards / power-ups.

  • Push other movable objects around.

These things can fail to happen, or happen in glitchy, inconsistent ways when you don't have a rigid body.

And so, you'll see a lot of advice that jumps directly to "always use a rigid body to move" because that's almost always the right answer, and they don't always clarify that it's based on these assumptions.

Your case isn't that though - you only need the collider for mouse picking / raycasting, and don't want any physics interactions with the other physics objects. So, your situation is the rare exception to the rule, where it's actually safe to skip the Rigidbody2D for this purpose.

Performance

Moving a Collier2D isn't free. Each time you change the transform, the engine needs to clone those changes into the Box2D simulation so that your raycasts and mouse picks work correctly.

In my tests, that sync-up adds something like .1-.2 ms to the frame time, but not per object. Most of the cost is doing the sync at all, and the per-object cost is much lower, so having a bunch of moving colliders doesn't compound the cost too badly.

In older versions of Unity, moving a collider without a rigid body was much more expensive, and so you'll see a lot of superstition and misinformation about it lingering around the net, saying that skipping the body will tank your performance. (I was guilty of this myself, until a user here pointed out to me that this issue was patched years ago)

The best way to see how the performance compares for your situation/version is to use the built-in profiler to measure it.

When I do that in a recent Unity 6 version, I find adding a Rigidbody2D to a moving Collider2D is still significantly more expensive when it's moving near other colliders than when I do the same test with no body. So you don't gain performance this way - at least not on desktop. To be sure, verify this on your own target platforms with a scene representative of your in-game use case.

To minimize any spurious collision-handling work in these tests, I had "Is Trigger" checked on all my colliders, and I put them all in a "No Collide" physics layer with its interactions disabled with all layers (including itself):

No-Collide layer collision matrix configuration in Project Settings > Physics 2D

Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

The advice you've found is a little oversimplified. There's an unstated assumption here:

If you have a moving object with a Collder2D and you want that movement to interact with other physics objects, then it should have a Rigidbody2D.

Very often that bolded part is implicit in the context. If someone is asking about moving a character in a platformer, or a vehicle in a driving / spaceflight game, then they almost certainly want the Collider2D to...

  • Stop the object from moving into / through solid obstacles.

  • Fire trigger & collision events when it hits enemies / hazards / power-ups.

These things can fail to happen, or happen in glitchy, inconsistent ways when you don't have a rigid body.

And so, you'll see a lot of advice that jumps directly to "always use a rigid body to move" because that's almost always the right answer, and they don't always clarify that it's based on these assumptions.

Your case isn't that though - you only need the collider for mouse picking / raycasting, and don't want any physics interactions with the other physics objects. So, your situation is the rare exception to the rule, where it's actually safe to skip the Rigidbody2D for this purpose.

Performance

Moving a Collier2D isn't free. Each time you change the transform, the engine needs to clone those changes into the Box2D simulation so that your raycasts and mouse picks work correctly.

In my tests, that sync-up adds something like .1-.2 ms to the frame time, but not per object. Most of the cost is doing the sync at all, and the per-object cost is much lower, so having a bunch of moving colliders doesn't compound the cost too badly.

In older versions of Unity, moving a collider without a rigid body was much more expensive, and so you'll see a lot of superstition and misinformation about it lingering around the net, saying that skipping the body will tank your performance. (I was guilty of this myself, until a user here pointed out to me that this issue was patched years ago)

The best way to see how the performance compares for your situation/version is to use the built-in profiler to measure it.

When I do that in a recent Unity 6 version, I find adding a Rigidbody2D to a moving Collider2D is still significantly more expensive when it's moving near other colliders than when I do the same test with no body. So you don't gain performance this way - at least not on desktop. To be sure, verify this on your own target platforms with a scene representative of your in-game use case.

To minimize any spurious collision-handling work in these tests, I had "Is Trigger" checked on all my colliders, and I put them all in a "No Collide" physics layer with its interactions disabled with all layers (including itself):

No-Collide layer collision matrix configuration in Project Settings > Physics 2D