Better way of detecting 2D/3D intersection?

So a few days ago I made this handy effect where any physical object intersecting this 2D line would highlight it. It works, but my concern is that it could get expensive very quickly because of how it calculates it behind the scenes.

It’s not fully obvious but the way it works is that it simulates a 2D hitbox for each object, and then casts a 2D raycast from both ends of the line to check if there’s an intersection.

The reason why I think it’s expensive is because it calls :WorldToViewportPoint a lot for creating the 2D hitbox, and a lot more calculations for the 2D raycast, every frame.

If anyone has any ideas on how to optimize this, let me know!

1 Like

I mean, computers are pretty powerful. Use script performance to check, maybe?

Well, I’d assume that you’re making the server do these calculations at all times. What you can do instead is make a slim cooldown for when it should make calculations which could make a slight difference I believe, if there is anything else I find I’ll keep you updated.

You could try using a Blockcast for this.
Basically, it casts the shape of a block in the given direction. So you can just make that block very thin.

Not sure if this is better for performance, but it definetly makes it simpler.


Best regards,
Pinker