How to insert collisions in a Path Shadow?

Hello guys, I’m very new in Unity and I’m with a little project for my Final Paper at University. To achieve my goal, first I need to accomplish one thing. Insert colliders in path shadows created by a Shadow Caster.

I want to do the exactly what was made in this video:

I was following this tread to implement the shadows all over my Tilemap: Tilemaps Shadow Caster (experiment)

BrianCraig’s script did a great job in my project, but I would need to edit his script to try to insert colliders on it and these colliders need to change along with the pathsahdow.

This is what I have at the moment.

I tried to manipulate meshs in order to achieve this, but it wasn’t working. Probably I was doing something wrong, even because I’m a real noob in Unity.

I’d be grateful if someone could, at least, try to help me. What I’m doing here is extremely important to my career, and would be awesome understand and accomplish these things.

I’ve attached the files here below if someone wants to help. Thanks in advance! :slight_smile:

7991643–1026909–Tilemap-Shadow-Caster-main.zip (5.33 KB)

I’m in charge of 2D physics here at Unity so first-up, welcome!

I have to say I do love this dynamic where an intangible thing such as a shadow becomes an interactive element such as a surface; makes me want to roll-up my sleeves and experiment!

So my description here might be a little complex if you’re relatively new to Unity but with that said, you sound like you’re not worried by complexity so if I can help, I will either here or DM me.

So maybe performance isn’t the first thing on your mind here but I wanted to bring to your attention some new features in Unity that I introduced in 2021.2 that will massively help you moving forward.

CustomCollider2D & PhysicsShapeGroup2D & Collider2D.GetShapes.

Essentially 2D physics only has 4 primitive shape types: Circle, Capsule, Polygon and Edge. The custom collider gives you direct access to creating these without any other hidden processes getting in the way. You can create any number of these shapes of varying kinds. You can also modify them directly, adding/removing them and changing their details. This is far faster then using things like the CompositeCollider2D or PolygonCollider2D which do an awful lot of work behind the scenes decomposing the paths (which are not physics things) into primitives, either edges or polygons.

For those “shadow platforms”, which to me look like they can simply be edges, you can add an edge to the CustomCollider2D. You can also add/remove as many edges as you like. Also edges have this feature which allows you to place them and specify the “adjacent” start/end points which are a ghost edge which is a hint to solver to ensure that you can smoothly move on/off them without catching the start/end points known as ghost collisions.

I put up a bunch of dev videos showing the CustomCollider2D being changed in realtime. You just couldn’t do this kind of thing for any other collider type:

Also, look in my signature you’ll see I maintain a Physics 2D examples repo here for all features here. There’s a few examples driving the CustomCollider2D you can find in there. Here’s a link to the scripts used in those examples that can give you some idea of usage:
https://github.com/Unity-Technologies/PhysicsExamples2D/blob/2021.2/Assets/Scripts/SceneSpecific/CustomCollider2D_Logo.cs
https://github.com/Unity-Technologies/PhysicsExamples2D/blob/2021.2/Assets/Scripts/SceneSpecific/CustomCollider2D_Compound.cs

Here I annotated your image to show the edge (purple) and the adjacent edges (in red). This would mean the character would gracefully move over the edge without any ghost collisions (catching the ends of the edge):

It might be worth experimenting here with the above because it would help you have an “overlay” as a CustomCollider2D for those walkable shadows where you can add/remove them at will or even have multiple ones of them so you can add/remove groups of them.

If you need any more information then ping me.

Thank you so much MelvMay! I’ll try to absorb everything that yoou pointed here. I’m certain that will help A LOT. I’ll contact you later if I have some problem.

And for all those who are following this thread, feel free to give your tips, it’s always good have options.

Cheers
Alef

1 Like

Hello MelvMay.

I tried to understand the things that you showed here before. I could understand your point, and I think that is the way, but I couldn’t understand how to use the Custom Collider. in my project.

Some questions:

  • I’ll work with the script that I mentioned before or there is a simpler way? PS.: I saw your 2D Collider Lights Shadow Test #3, and the shadows there are perfect. The blocks were Tilemaps? In positive case, how did you manage that? Because using Shadow Caster 2D I could only create shadows based in one Tilemap, not all of them separated.
  • How am I going to use Custom Collider with the script that I used? Or, of course, with the method that you think it’s better?

I know you must be very busy, but if you have some time, could you show me an example?

Cheers
Alef

The view you linked to is the new shadow code that we’re hoping to land in 2022.2 (not confirmed). That shadow code isn’t specific to any type of collider, it works for all 2D colliders. It does this because it uses some efficient methods to read/cache the shape data via the Collider2D.GetShapes, performs shape culling and passes it to the shadow code.

I’m not sure how to help further. The example project I listed above shows you the CustomCollider2D being used. You can add as many continuous edges as you like.

I don’t know what you don’t know. I obviously cannot just tell you how to use it. All I can say is that the CustomCollider2D allows you to quickly add/remove continuous edges which you can use to add shadow edges you can walk on or remove/update them later in realtime. Examples of that are in the project.

I think you have to ask me a specific question on something you’re struggling with because I’m not sure what else to say.