0
\$\begingroup\$

I'm trying to have objects in a game be selectable only from a top down orthographic camera view. I think having physics colliders might be overkill for that - especially because this only needs to be in 2D and not 3D. Is there another method that might be better, such as something like a renderer "raycasts", but perhaps even more performant as it is just from the orthographic 2D top down view

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

If you build your "top down" view on the XY plane, you can use 2D colliders and Physics2D.OverlapPoint.

This is about as efficient as you will get. A 2D collider is using exactly the same math you'd need to write in your C# code to test for a hit anyway. And if you're using Kinematic bodies on a physics layer that doesn't interact with anything, they won't burn any cycles on unneeded collision detection and resolution.

\$\endgroup\$
3
  • \$\begingroup\$ is there a way to automatically create 2D colliders from the top down for 3D meshes? \$\endgroup\$
    – ina
    Commented Dec 18, 2020 at 1:16
  • \$\begingroup\$ You could write a script to do it reasonably easily. Project all the triangles and make a polygon collider out of their perimeter. \$\endgroup\$
    – DMGregory
    Commented Dec 18, 2020 at 1:19
  • 1
    \$\begingroup\$ Or you could use GetComponent<Renderer>().bounds which gets you the bounding box of the object as its used for culling calculations and create a BoxCollider2D out of it. \$\endgroup\$
    – Philipp
    Commented Dec 18, 2020 at 9:12
-1
\$\begingroup\$

you can use quadtree algorithm to detect which object under the mouse position

you can use octree algorithm in 3d space

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Why would you do that when Unity already provides that for you? \$\endgroup\$
    – Philipp
    Commented Dec 18, 2020 at 9:10
  • \$\begingroup\$ some people like inventing the sledgehammer even though it is now commodity that's basically free \$\endgroup\$
    – ina
    Commented Dec 18, 2020 at 23:51
  • \$\begingroup\$ Some times you dont need whole physics features with heavy calculations \$\endgroup\$
    – Kam Rad
    Commented Dec 19, 2020 at 22:14
  • \$\begingroup\$ Im using ECS and i had to use this algorithm for nearby object detection \$\endgroup\$
    – Kam Rad
    Commented Dec 19, 2020 at 22:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .