2
\$\begingroup\$

Typically when I want to define an irregularly-shaped "area" in my 3D game (e.g. for events to trigger), I'll compose it using various 3D colliders and treat them as a group.

Something that could speed up my workflow is to instead use the new Unity Splines package to define an irregular area on my ground, since it is so much easier to "draw" with - far more convenient than the classic collider workflow.

Of course even if you could, the result wouldn't have volume. My idea is that I don't care about the vertical axis - I only want to know whenever the player's X and Z are within the bounds defined by the spline's projection onto the XZ plane.

Is there a reasonable way to use the new Unity Splines package to generate "areas" with the following features?

  • Tell me whether a point or collider is within the area's XZ projected plane.
  • Generate random points within the area.

I've browsed its API but I can't think of a good way to do achieve this.

At the moment I'm considering that a possible alternative is to use the 2D package to create a plane projection parallel to my terrain, and then draw a 2D polygon collider and virtually update a point on that plane representing the player's XZ on the projection since the polygon collider does have an API to detect collisions - that would speed up my area-making-workflow, but it feels fairly overengineered or even hacky compared to putting lots of 3D colliders.

My ideal solution is to quickly draw areas with splines and then just reference the Spline object to query whether the player is inside or not. Or, at the very least, have a button to generate colliders within that spline but I still can't see a way to do that with the current API, that I am aware of.

\$\endgroup\$
4
  • 2
    \$\begingroup\$ It sounds to me like you want to ask "How to detect when the player has entered an irregularly-shaped area?" - you have one idea of how to do it, using splines, but that might not be the best solution. Using that proposed solution as the basis of your question will tend to attract only answers about that method, and exclude alternatives. So if you don't have a hard requirement to solve this with Unity splines, I'd recommend rephrasing your question to ask for a solution to the root problem without presuming that solution should use that specific library. \$\endgroup\$
    – DMGregory
    Commented Dec 16, 2022 at 18:07
  • \$\begingroup\$ @DMGregory I definitely want to somehow use Splines because of their ease of use when "drawing" areas. The solutions such as collider composition or 2D projection work but they make my life miserable when setting up the many (very) irregular areas I want. \$\endgroup\$
    – Saturn
    Commented Dec 17, 2022 at 4:26
  • \$\begingroup\$ It would be much easier, computationally wise, to use AABB, quadtree, or octree for broad detection and a math formula for the local check. Splines are interpolated, fine for drawing, but making them much more complicated to calculate collisions upon. Sorry, little bit of misery on your part could make the difference between playable and unplayable on lower-end hardware, \$\endgroup\$
    – user122973
    Commented Dec 22, 2022 at 3:28
  • \$\begingroup\$ Why not use the splines to generate a collider programatically? \$\endgroup\$ Commented May 12 at 9:42

0

You must log in to answer this question.

Browse other questions tagged .