I’ve done a lot of looking through forums but noone seems to have the same issue.
I instantiate a prefab (SlidingBarrier) as a GameObject which has a box collider. I randomly position the object. Then I check if the bounds of the object intersects with the bounds of another GameObject that I had earlier instantiated from a different prefab (GoalStalker). This check always returns true. At this point I print the position of the bounds.
I have setup another check for these bounds in the OnDrawGizmo() method. The bounds are drawn with a wire cube in magenta if they aren’t intersecting, and yellow if they are. I then print the position and size of the bounds.
The second set of positions (from OnDrawGizmo) are different to the first, but only on the x and z axes, (I assume only these axes because I force the y to remain the same as prefab).
My question is: why does the position of the bounds in the first check not get updated, but they are in the second check? And how can I make it so the bounds do get updated before the first check?
Here’s my code:
public void Start()
{
hazards = new List<GameObject>();
GameObject gameControllerObject = GameObject.FindWithTag("GameController");
if (gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent<GameController>();
}
SetHolePosition();
BuildFloors();
AddHazards();
}
private void AddHazards()
{
RemoveWalls();
AddGoalStalker();
AddSlidingBarrier();
}
private void AddGoalStalker()
{
Vector3 pos = new Vector3(hole.transform.position.x, goalStalkerPrefab.transform.position.y, hole.transform.position.z);
InstantiateObject(goalStalkerPrefab, hazardType.GoalStalker, pos);
}
private void AddSlidingBarrier()
{
Vector3 pos = RandomPos(hazardType.SlidingBarrier);
pos.y = hazardType.SlidingBarrier.minPos.y;
InstantiateObject(slidingBarrierPrefab, hazardType.SlidingBarrier, pos);
}
private void InstantiateObject(GameObject prefab, Hazard hazard, Vector3 pos)
{
string hazardName = hazard == hazardType.GoalStalker ? "GoalStalker" : "SlidingBarrier";
int round = gameController.RoundsCreated() + 1;
int randInt = Random.Range(0, round);
if (round >= hazard.firstRound && randInt >= hazard.chance)
{
GameObject obj = Instantiate(prefab, hazardDirectory, false) as GameObject;
float rot = RandomRot(hazard);
obj.transform.position = hazardDirectory.position + pos;
obj.transform.Rotate(Vector3.up, rot, Space.Self);
bool checkIntersect = CheckIntersect(obj);
hazards.Add(obj);
}
}
private bool CheckIntersect(GameObject obj)
{
Bounds bounds = obj.GetComponent<Collider>().bounds;
foreach (GameObject other in hazards)
{
bool intersects = bounds.Intersects(other.GetComponent<Collider>().bounds);
Debug.Log("checkIntersect = " + intersects + "; at center " + bounds.center.ToString() + "; with size " + bounds.size.ToString() + ";");
if (intersects)
{
return true;
}
}
return false;
}
private void OnDrawGizmos()
{
Gizmos.color = Color.magenta;
foreach (GameObject hazard in hazards)
{
Bounds bounds = hazard.GetComponent<Collider>().bounds;
foreach (GameObject other in hazards)
{
if (hazard != other)
{
if (bounds.Intersects(other.GetComponent<Collider>().bounds))
{
Gizmos.color = Color.yellow;
}
}
}
if (hazard.name == "SlidingBarrier(Clone)")
{
Debug.Log("OnDrawGizmos() - bounds at center " + bounds.center.ToString() + "; with size " + bounds.size.ToString() + ";");
}
Gizmos.DrawWireCube(bounds.center, bounds.size);
}
}