Hi, my problem is that the bounds.Intersects doesn’t work when I check it (or is just not triggering a move).
What I’m trying to do is, I have generated many “cells” and those cells have BoxColliders attached to them, but when they get instantiated the size of the collider is determined.
Here is my Cell script (generating a quad just for visual representation so I can see whether or not it overlaps):
using UnityEngine;
using System.Collections;
public class CellSize : MonoBehaviour {
public int x, z;
void Start() {
BoxCollider collider = this.gameObject.GetComponent<BoxCollider>();
collider.size = new Vector3(x, 1, z);
GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
quad.transform.parent = this.transform;
quad.transform.position = this.transform.position;
quad.transform.eulerAngles = new Vector3(90, 0, 0);
quad.transform.localScale = new Vector3(x, z, 0);
}
And this is the part of code that I have in my GenerationManager script that should move the cells away from each other if they are intersecting:
private void Separate(Transform childObj) {
BoxCollider childObjCollider = childObj.GetComponent<BoxCollider>();
foreach (Transform child in this.transform) {
BoxCollider childCollider = child.GetComponent<BoxCollider>();
if (childObj.transform.name != child.transform.name) {
while (childObjCollider.bounds.Intersects(childCollider.bounds)) {
childObj.position += new Vector3(5, 0, 5);
//childObj.position = child.transform.position + ((childObj.transform.position - child.transform.position).normalized * 5f);
}
}
}
}
But apparently it still doesn’t move the objects away from each other, but I put a debug.log inside the while loop, so I’m assuming the colliders know they are intersecting, but the objects just aren’t moving.
Here’s a picture showing the end result of this:
As you can see the colliders are still overlapped.