0
\$\begingroup\$

My Player sprite needs to collide with some objects and be unable to pass through them and in other cases detect when it passes through some object (e.g. some non-fatal enemies and some one-way barriers).

How do I test what an object is and make enforce one collision type or the other?

\$\endgroup\$
  • \$\begingroup\$ I don't understand, are you saying that for a given object, the player can either pass through or being hit according to situations, or that for some objects it can pass through and for others it cannot? \$\endgroup\$ – FSic Oct 1 at 9:18
  • \$\begingroup\$ @FSic The latter. There are some barriers that are solid and immovable, some that can be passed through one-way only and some that just have affect the player if they move over each other (2D only). \$\endgroup\$ – BanksySan Oct 1 at 10:33
1
\$\begingroup\$

Effectors! You could create two different kinds of gameObjects:

  • One is a normal barrier, so you can just attach a collider2D and make sure that in the layers interaction matrix you allow interactions between the layer the player is in and the barriers' one;

  • The other gameObject is similar but you additionally attach the PlatformEffector2D component to it (be sure to tick the option "used by effector" in the collider2D component). You can then tweak how the one-way barrier will work through the inspector (direction, area of effectiveness etc.).

Here you have a good video that explains how the effectors work.

\$\endgroup\$
  • \$\begingroup\$ I hadn't heard of them before. They look like exactly what I want. I'll get a quick PoC done and then give you a lovely greet tick. Thanks! \$\endgroup\$ – BanksySan Oct 1 at 17:45
  • \$\begingroup\$ Are effectors 2D only? \$\endgroup\$ – BanksySan Oct 1 at 18:14
  • \$\begingroup\$ As far as I know yes, I don't know if there is an equivalent 3D component. \$\endgroup\$ – FSic Oct 2 at 6:48
1
\$\begingroup\$

you have you have all events and callbacks for Both trigger and collider. for example OnTriggerEnter() and OnCollisionEnter() but if you want to use trigger event on a collider behavior, you can have duplicate collider component with one one them checked as trigger. you can make scale of trigger a little bigger for detection

\$\endgroup\$
  • \$\begingroup\$ That's actually quite a nice fix. \$\endgroup\$ – BanksySan Oct 1 at 17:04
1
\$\begingroup\$

You can also do this with just vanilla collider setup and physics layers.

Make two layers in your Tags and Layers settings:

  • PlayerCollision — represents a solid player character
  • PlayerTrigger — represents trigger-only interactions with player characters

In your physics settings, tick the boxes so that the PlayerCollision layer does not collide with PlayerTrigger. Both layers can still collide with themselves, with default, etc.

Give your player character prefab two child objects with colliders attached: one on the PlayerCollision layer, and one on the PlayerTrigger layer. Set isTrigger to true on the second one.

Now, anything that should stop the player can go on the default layer — it will interact with the non-trigger PlayerCollision collider on the player character and halt it.

Anything that should not stop the player should go on the PlayerTrigger layer. It will interact with the player's trigger collider and send trigger messages, without triggering a physical collision resolution.

These entities can still physically interact with other entities on the default layer (if you don't want them colliding with anything, then just set the entity's own collider's isTrigger to true instead)

This gives you the flexibility to make the player's trigger volume different than their collision volume, if that's useful for your needs. And it correctly filters all the physics behaviours without adding extra component types into the mix.

\$\endgroup\$

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.