Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

Hex map: keep the prefab root rotation when units turn (Fixes #2006) (#2008)

Beriaf880c4088bb
diff --git a/changelog.d/2006-prefab-root-rotation.md b/changelog.d/2006-prefab-root-rotation.md
new file mode 100644
index 000000000..a604b8e63
--- /dev/null
+++ b/changelog.d/2006-prefab-root-rotation.md
@@ -0,0 +1,4 @@
+### Fixed
+- Hex map: a unit or placed figure whose model carries its own root rotation (the Collection vehicles' import fix) keeps
+ it when it turns, so the Willys jeep, the Sandwich truck, the horse wagon and the citrus truck will stand on their
+ wheels instead of lying on their side. Players see it once the alias rows that draw those vehicles land (#1955) (#2006).
diff --git a/unity/Assets/Scripts/Map/HexTiles/Core/HexBoardBuilder.cs b/unity/Assets/Scripts/Map/HexTiles/Core/HexBoardBuilder.cs
index 2177da8f9..83d1edf28 100644
--- a/unity/Assets/Scripts/Map/HexTiles/Core/HexBoardBuilder.cs
+++ b/unity/Assets/Scripts/Map/HexTiles/Core/HexBoardBuilder.cs
@@ -440,7 +440,8 @@ namespace Statecraft.HexTiles
var go = Object.Instantiate(prefab, parent);
var b = HexModels.Bounds(go);
float k = HexModels.Normaliser(b.size, p.Fit).x;
- go.transform.localPosition = p.Position; go.transform.localRotation = p.Matrix.rotation;
+ // Turn the prefab's own root rotation (an import fix), never replace it (#2006).
+ go.transform.localPosition = p.Position; go.transform.localRotation = p.Matrix.rotation * go.transform.localRotation;
go.transform.localScale = Vector3.one * k * p.Matrix.lossyScale.x;
foreach (var a in go.GetComponentsInChildren<Animator>(true)) HexStandIns.SampleIdle(a);
continue;
diff --git a/unity/Assets/Scripts/Map/HexTiles/Core/HexUnits.cs b/unity/Assets/Scripts/Map/HexTiles/Core/HexUnits.cs
index b3567ca9e..87fe28223 100644
--- a/unity/Assets/Scripts/Map/HexTiles/Core/HexUnits.cs
+++ b/unity/Assets/Scripts/Map/HexTiles/Core/HexUnits.cs
@@ -88,7 +88,10 @@ namespace Statecraft.HexTiles
/// clips are exported in place, so position is the code's and every move ends exactly on a hex centre.</summary>
public sealed class HexUnitMotion
{
- sealed class Live { public HexUnitMember M = null!; public GameObject Go = null!, Rig = null!; public AnimationClip? Move, Idle; public float Scale; public GameObject? Prefab; }
+ sealed class Live { public HexUnitMember M = null!; public GameObject Go = null!, Rig = null!; public AnimationClip? Move, Idle; public float Scale; public GameObject? Prefab;
+ /// <summary>The model's root rotation as instantiated (a Collection vehicle's import fix, -90 degrees about X);
+ /// Pose turns it about the vertical instead of replacing it, or the model lies on its side (#2006).</summary>
+ public Quaternion Authored = Quaternion.identity; }
readonly List<Live> live = new List<Live>();
readonly List<Vector3> path;
@@ -157,7 +160,7 @@ namespace Statecraft.HexTiles
l.Go.SetActive(false);
if (Application.isPlaying) Object.Destroy(l.Go); else Object.DestroyImmediate(l.Go);
}
- l.Prefab = prefab; l.Go = go; l.Rig = animator != null ? animator.gameObject : go; l.Scale = scale;
+ l.Prefab = prefab; l.Go = go; l.Authored = go.transform.localRotation; l.Rig = animator != null ? animator.gameObject : go; l.Scale = scale;
l.Move = clips.FirstOrDefault(c => c.name.IndexOf("walk", StringComparison.OrdinalIgnoreCase) >= 0)
?? clips.FirstOrDefault(c => c.name.IndexOf("idle", StringComparison.OrdinalIgnoreCase) < 0);
l.Idle = clips.FirstOrDefault(c => c.name.IndexOf("idle", StringComparison.OrdinalIgnoreCase) >= 0);
@@ -196,7 +199,7 @@ namespace Statecraft.HexTiles
var p = centre + turn * new Vector3(l.M.Offset.x * size, 0f, l.M.Offset.y * size);
p.y = surface.Height(p);
// Board space: the units' parent sits in the board root (or at the origin for a board on its own).
- l.Go.transform.localPosition = p; l.Go.transform.localRotation = Quaternion.Euler(0f, heading + ModelYaw + l.M.YawOffset, 0f);
+ l.Go.transform.localPosition = p; l.Go.transform.localRotation = Quaternion.Euler(0f, heading + ModelYaw + l.M.YawOffset, 0f) * l.Authored;
var clip = moving ? l.Move ?? l.Idle : l.Idle ?? l.Move;
if (clip == null) continue;
float cycle = l.M.CycleShare * size;
diff --git a/unity/Assets/Tests/EditMode/HexCollectionFiguresTests.cs b/unity/Assets/Tests/EditMode/HexCollectionFiguresTests.cs
index e44fec7b6..7ff4fca98 100644
--- a/unity/Assets/Tests/EditMode/HexCollectionFiguresTests.cs
+++ b/unity/Assets/Tests/EditMode/HexCollectionFiguresTests.cs
@@ -140,6 +140,44 @@ public sealed class HexCollectionFiguresTests
Assert.That(requests, Is.EqualTo(1));
}
+ /// <summary>A Collection vehicle's prefab root carries its import fix (the FBX Z-up to Y-up -90 degrees about X, as on
+ /// the Willys jeep, #2006). Placement must turn that root about the vertical only, never replace it, or the model lies
+ /// on its side.</summary>
+ static readonly Quaternion ImportFix = Quaternion.Euler(-90f, 0f, 0f);
+
+ static void AssertKeepsImportFix(Transform t, float yaw, string what)
+ {
+ var want = Quaternion.Euler(0f, yaw, 0f) * ImportFix;
+ Assert.That(Quaternion.Angle(t.localRotation, want), Is.LessThan(0.1f), $"{what}: {t.localRotation.eulerAngles} wanted {want.eulerAngles}");
+ }
+
+ [Test] public void UnitTurnsAboutTheVerticalAndKeepsThePrefabRootRotation()
+ {
+ var prefab = Model(); prefab.transform.localRotation = ImportFix;
+ HexCollectionFigures.Source = (_, _, done) => done(prefab);
+ foreach (var heading in new[] { 30f, 120f })
+ {
+ var unit = new HexUnit { Members = new List<HexUnitMember> { new HexUnitMember { Model = Scout, StandIn = StandIn.Box, YawOffset = 90f } } };
+ var motion = new HexUnitMotion(unit, new[] { (0, 0) }, new FlatSurface(), Root().transform, facingDeg: heading);
+ motion.Pose(0f, false);
+ AssertKeepsImportFix(motion.Objects.Single().transform, heading + 90f, $"unit heading {heading}");
+ }
+ }
+
+ [Test] public void SkinnedPlacementTurnsAboutTheVerticalAndKeepsThePrefabRootRotation()
+ {
+ var prefab = Model(); prefab.transform.localRotation = ImportFix;
+ var skin = new GameObject("skin"); skin.transform.SetParent(prefab.transform, false);
+ skin.AddComponent<SkinnedMeshRenderer>().sharedMesh = prefab.GetComponent<MeshFilter>().sharedMesh;
+ foreach (var heading in new[] { 30f, 120f })
+ {
+ var root = Root(); var assets = root.AddComponent<HexBoardAssets>();
+ var p = new HexPlacement("probe", StandIn.Box, Fit.Height, Color.white, Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0f, heading, 0f), Vector3.one * 20), 0, 0);
+ var layer = HexBoardRenderer.Emit(root.transform, new[] { p }, assets, "probe", _ => prefab);
+ AssertKeepsImportFix(layer.GetChild(0), heading, $"placement heading {heading}");
+ }
+ }
+
[Test] public void UnitTeardownIgnoresLateArrival()
{
var root = Root(); var assets = root.AddComponent<HexBoardAssets>();