Search Unity

Divide a collider

Discussion in 'Physics' started by Geckoo, Sep 8, 2016.

  1. Geckoo

    Geckoo

    Joined:
    Dec 7, 2014
    Posts:
    11
    I am wondering if it could be possible to divide a big cube collider into (sub)colliders. As an example, I have a cube collider with these dimensions 4*4*4. So I would like to create 64 (sub)colliders with a dimension 1*1*1. In my project, this way I could simplify the creation of a large invisible floor which lights up when player walks on. Any idea? Thank you ++
     
  2. Geckoo

    Geckoo

    Joined:
    Dec 7, 2014
    Posts:
    11
    OK. I did this code so as to split a big box collider into subcolliders. Put it on a transform with a BoxCollider component. Set your prefab 1*1*1 - like a simple cube. It works fine, but I think that it could be improved. Do you see something that I should change? Thank you ++

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class SubCollider : MonoBehaviour {
    6.  
    7.   public Transform cube;
    8.   GameObject[] cubes;
    9.   // Vector3
    10.   int gridX;
    11.   int gridY;
    12.   int gridZ;
    13.   // our box collider
    14.   BoxCollider bc;
    15.   // even or odd?
    16.   float rtnX;
    17.   float rtnY;
    18.   float rtnZ;
    19.  
    20.   void Awake()
    21.   {  // find the box collider
    22.   bc = this.transform.GetComponent<BoxCollider>();
    23.   // check collider scale
    24.   if (bc != null)
    25.   {
    26.   gridX = (Mathf.RoundToInt(this.transform.localScale.x));
    27.   gridY = (Mathf.RoundToInt(this.transform.localScale.y));
    28.   gridZ = (Mathf.RoundToInt(this.transform.localScale.z));
    29.   }
    30.   // compute if X, Y or Z scale are even or odd
    31.   if (gridX % 2 == 1)
    32.   rtnX = 0.0f;
    33.   else
    34.   rtnX = 0.5f;
    35.  
    36.   if (gridY % 2 == 1)
    37.   rtnY = 0.0f;
    38.   else
    39.   rtnY = 0.5f;
    40.  
    41.   if (gridZ % 2 == 1)
    42.   rtnZ = 0.0f;
    43.   else
    44.   rtnZ = 0.5f;
    45.   }
    46.  
    47.   void Start()
    48.   {  // display for each integer/Vector3 inside the box collider a sub object
    49.   for (int x = 0; x < gridX; x = x + 1)
    50.   {
    51.   for (int y = 0; y < gridY; y = y + 1)
    52.   {
    53.   for (int z = 0; z < gridZ; z = z + 1)
    54.   Instantiate(cube, new Vector3((transform.position.x + x - (Mathf.FloorToInt(gridX/2)) + rtnX), (transform.position.y + y - (Mathf.FloorToInt(gridY/2)) + rtnY), (transform.position.z + z - Mathf.FloorToInt(gridZ/2)) + rtnZ), Quaternion.identity);
    55.   }
    56.   }
    57.   // check all interactable objects and give them a unique parent to keep project clean
    58.   cubes = GameObject.FindGameObjectsWithTag("Interactable");
    59.   // hierarchy
    60.   foreach (GameObject cc in cubes)
    61.   cc.transform.parent = this.transform;
    62.   // disable main collider and script at end
    63.   bc.enabled = false;
    64.   this.enabled = false;
    65.   }
    66. }
    67.  
     
unityunity