Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Looking for a quick way to surround stages with box colliders

Discussion in 'Scripting' started by flipwon, Mar 20, 2022.

  1. flipwon

    flipwon

    Joined:
    Dec 29, 2016
    Posts:
    179
    I am currently making many stages for my game and am needing to manually surround the stages with box colliders. I need all sides to have colliders because of the type of game it is.

    The stages can be all sorts of shapes but is 100% right angles. An example shape:

    upload_2022-3-20_14-22-31.png

    Any ideas on a clever way to surround something like this with box colliders at runtime?
     

    Attached Files:

  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    How are you defining the shapes? Are they meshes?
     
  3. flipwon

    flipwon

    Joined:
    Dec 29, 2016
    Posts:
    179
    A combination of meshes yes, they're basically pipes, pretty much as pictured.
     
  4. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,920
    If the outside is always a rectangle you could do a boxcast from each direction to find the limit of that side. Assuming your meshes are somehow colliders. You'd end up with say, x0=5, x1=40, z0=-10, z1=20. So now you know the left wall should be at x=4.5 (assuming it's 1 unit thick) and z=5 (average of the z's) and at least 30 units wide (but I tend to simply make my border walls 100 units long, not worry about having them centered, and let the ends stick out super-far; so all I need is x=4.5, z=leave at 0).

    Or you could run through the mesh points to get that same thing. Easy for one mesh which is always at 000, but if you have several in various spots you'll need to add their positions (like M[2].position.x + M[2].verts.x)