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

Question Wrong collider on a procedural quad

Discussion in 'Scripting' started by TTuPC, May 11, 2022.

  1. TTuPC

    TTuPC

    Joined:
    Mar 22, 2021
    Posts:
    36
    I generate a procedural quad and I want to add a box collider to it.
    The problem is that when I add the collider via script the collider is a default box collider (1x1x1) placed at the origin:

    upload_2022-5-11_20-26-25.png

    My code to attach the collider is:

    Code (CSharp):
    1.  
    2.             Collider wantTrigger = GameObject.AddComponent<BoxCollider>();
    3.             wantTrigger.isTrigger = true;
    4.  
    And if I try to add manually a box collider on play on a rotated quad I receive this:

    upload_2022-5-11_20-28-36.png

    So I receive a bigger box collider because it doesn't rotate with the mesh.

    Someone can tell me how to apply via script a box collider to the plane in the right way?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,757
    The box collider can be offset and scaled.

    It cannot be rotated.

    To rotate a box collider to match your Quad, you would need a second GameObject (perhaps as a child?) with its Tranform set to have the necessary rotation to match the alignment of your geometry quad.

    You could also perhaps use a MeshCollider and just feed the quad in directly. Note: MeshColliders only collide against the "outside" of the mesh, eg, the visible portion, so if the above is a flat one-sided quad, you'd need to duplicate the mesh faces to have it collide from either side.

    If you're interested in seeing other procgen stuff, check out my MakeGeo project:

    MakeGeo is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/makegeo

    https://github.com/kurtdekker/makegeo

    https://gitlab.com/kurtdekker/makegeo

    https://sourceforge.net/p/makegeo
     
  3. TTuPC

    TTuPC

    Joined:
    Mar 22, 2021
    Posts:
    36
    Perfect! Thank you so much!