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 444. So I would like to create 64 (sub)colliders with a dimension 111. 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 ++
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 111 - 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 ++
using UnityEngine;
using System.Collections;
public class SubCollider : MonoBehaviour {
public Transform cube;
GameObject[] cubes;
// Vector3
int gridX;
int gridY;
int gridZ;
// our box collider
BoxCollider bc;
// even or odd?
float rtnX;
float rtnY;
float rtnZ;
void Awake()
{ // find the box collider
bc = this.transform.GetComponent<BoxCollider>();
// check collider scale
if (bc != null)
{
gridX = (Mathf.RoundToInt(this.transform.localScale.x));
gridY = (Mathf.RoundToInt(this.transform.localScale.y));
gridZ = (Mathf.RoundToInt(this.transform.localScale.z));
}
// compute if X, Y or Z scale are even or odd
if (gridX % 2 == 1)
rtnX = 0.0f;
else
rtnX = 0.5f;
if (gridY % 2 == 1)
rtnY = 0.0f;
else
rtnY = 0.5f;
if (gridZ % 2 == 1)
rtnZ = 0.0f;
else
rtnZ = 0.5f;
}
void Start()
{ // display for each integer/Vector3 inside the box collider a sub object
for (int x = 0; x < gridX; x = x + 1)
{
for (int y = 0; y < gridY; y = y + 1)
{
for (int z = 0; z < gridZ; z = z + 1)
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);
}
}
// check all interactable objects and give them a unique parent to keep project clean
cubes = GameObject.FindGameObjectsWithTag("Interactable");
// hierarchy
foreach (GameObject cc in cubes)
cc.transform.parent = this.transform;
// disable main collider and script at end
bc.enabled = false;
this.enabled = false;
}
}