Code fix to make sure towers are flush with the ground

This may need to be changed later if we have any towers that have visuals that go underneath the ground. Right now that won't work, but we also don't have anything like that.
This commit is contained in:
Matt F 2026-06-25 11:36:31 -07:00
parent 699ea34268
commit 1afc78d64f

View file

@ -601,9 +601,9 @@ namespace TD.Gameplay
} }
Vector3 spawnPos = GridCoordinates.GetFootprintCenterWorld(anchor, def.FootprintSize); Vector3 spawnPos = GridCoordinates.GetFootprintCenterWorld(anchor, def.FootprintSize);
// Towers sit on the buildable plane (Y=0); raise slightly so the mesh base // Place on the buildable plane (Y=0); SeatOnGround below corrects the final Y
// sits flush rather than half-clipped. A cube of scale 1 needs +0.5 on Y. // from the mesh bounds so the tower rests flush regardless of its pivot.
spawnPos.y = 0.5f; spawnPos.y = 0f;
var go = Instantiate(def.TowerPrefab, spawnPos, Quaternion.identity); var go = Instantiate(def.TowerPrefab, spawnPos, Quaternion.identity);
var instance = go.GetComponent<TowerInstance>(); var instance = go.GetComponent<TowerInstance>();
@ -626,9 +626,32 @@ namespace TD.Gameplay
Destroy(go); Destroy(go);
return; return;
} }
// Seat the tower flush on the ground from its mesh bounds — BEFORE Spawn so the
// corrected transform is captured in the spawn sync. Handles any pivot.
SeatOnGround(go, groundY: 0f);
netObj.Spawn(destroyWithScene: true); netObj.Spawn(destroyWithScene: true);
} }
/// <summary>
/// Shifts <paramref name="go"/> vertically so the lowest point of its combined
/// renderer bounds rests at <paramref name="groundY"/>. Lets towers be authored with
/// any mesh pivot (center, base, etc.) and still sit flush on the ground — no
/// per-prefab Y tweaking. No-op if the prefab has no renderers.
/// </summary>
private static void SeatOnGround(GameObject go, float groundY)
{
var renderers = go.GetComponentsInChildren<Renderer>();
if (renderers.Length == 0) return;
Bounds b = renderers[0].bounds;
for (int i = 1; i < renderers.Length; i++)
b.Encapsulate(renderers[i].bounds);
go.transform.position += Vector3.up * (groundY - b.min.y);
}
// ----- Utility ---------------------------------------------------- // ----- Utility ----------------------------------------------------
private static bool TryGetDefinition(int typeId, out TowerDefinition def) private static bool TryGetDefinition(int typeId, out TowerDefinition def)