My current project is a turn-based board game in Unity. Here is what I am trying to accomplish and the issue I am having: In Start, I generate the board, then populate the surrounding area with decorations (trees, rocks, etc...), without overlapping with the board itself. To accomplish this, I essentially sample lots of points and use Physics.CheckSphere for overlap with the board, and only add an object if there is no overlap.
It all works perfectly well - except if before populating the area with decorations I translate or rotate the board. The Physics.CheckSphere calls treats it is as if the translation / rotations have not been made. What's strange is that a Debug call shows that the Physics.CheckSphere is indeed happening AFTER the board is translated / rotated. My guess is that since I am translating / rotating things in Start, the Physics system has not processed that the colliders have moved, and thus is still detecting them in their original positions. Is that correct, and is there any fix / alternative?
GameStateManager:
void Start()
{
BoardManager.GenerateBoard(raceSettings.Stage);
environmentManager.Generate(raceSettings.Stage.Environment, cameraController.Bounds);
}
BoardManager:
public void GenerateBoard(Stage stage)
{
_board = new();
Transform end = this.transform;
foreach(TrackSetting trackSetting in stage.Tracks)
{
GameObject newTrack = Instantiate(trackSetting.track, end.position, end.rotation);
newTrack.transform.parent = this.transform;
TrackTile track = newTrack.GetComponentInChildren<TrackTile>();
end = track.End;
}
CalculateBounds();
}
void CalculateBounds()
{
_bounds = new();
foreach(Transform square in this.transform)
{
foreach (Transform t in square.transform)
{
_bounds.Encapsulate(t.position);
}
}
if (_bounds.size.z > _bounds.size.x)
{
//transform.rotation = Quaternion.Euler(90f * Vector3.up);
//transform.SetPositionAndRotation(transform.position, Quaternion.Euler(90f * Vector3.up));
transform.Rotate(Vector3.up, 90f);
Debug.Log("ROTATE");
CalculateBounds();
}
}
EnvironmentManager:
void SpawnEnvironment(EnvironmentSettings es, Bounds bounds)
{
Debug.Log("BUILD WORLD");
worldBounds = bounds;
worldBounds.Expand(boundPadding);
List<Vector2> points = PoissonDiscSampling.GeneratePoints(es.Range, worldBounds, rejectionSamples);
foreach (Vector2 point in points)
{
Vector3 newPosition = new Vector3(point.x, 0f, point.y);
if (Physics.CheckSphere(newPosition, es.Clearance)) continue;
GameObject environmentalObject = GameObject.Instantiate(defaultTreePrefab);
environmentalObject.transform.position = newPosition;
environmentalObject.transform.parent = this.transform;
}
}