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.

Unity: a click on a unit's badge selects the unit (Fixes #1341) (#1349)

Eyal Golan374e999b979f
diff --git a/changelog.d/unit-badge-click-1341.md b/changelog.d/unit-badge-click-1341.md
new file mode 100644
index 000000000..6e4c857dd
--- /dev/null
+++ b/changelog.d/unit-badge-click-1341.md
@@ -0,0 +1,5 @@
+### Fixed
+
+- Clicking a unit's badge, the dark square with its symbol that floats over the unit, now opens that unit's card with its
+ orders, as clicking the unit itself does. Before, the click went to the land behind the badge, so the card showed that
+ hex and a unit could not be given a Move from its badge (#1341).
diff --git a/unity/Assets/Scripts/Map/MapPointer.cs b/unity/Assets/Scripts/Map/MapPointer.cs
index 565be225f..97ba08a1f 100644
--- a/unity/Assets/Scripts/Map/MapPointer.cs
+++ b/unity/Assets/Scripts/Map/MapPointer.cs
@@ -120,7 +120,8 @@ namespace Statecraft.Game.Map
}
/// <summary>The hex a click at <paramref name="pos"/> (screen pixels) would pick, or null over the UI or off the map:
- /// a pill's city hex (PillHex), else the hex of the terrain under the pointer. GameRoot also asks it for the hover.</summary>
+ /// a pill's city hex (PillHex), else the hex of the unit token drawn under the pointer (UnitLayer.TokenAtScreen), else
+ /// the hex of the terrain under the pointer. GameRoot also asks it for the hover.</summary>
public (int q, int r)? HexAt(Vector2 pos) => HexAt(pos, IsOverUi != null && IsOverUi(pos), true);
(int q, int r)? HexAt(Vector2 pos, bool overUi, bool pills)
@@ -129,6 +130,11 @@ namespace Statecraft.Game.Map
if (Map == null || Map.Terrain == null) return null;
if (pills && PillHex?.Invoke(pos) is (int, int) pill) return pill;
var cam = Camera.main;
+ // A unit's badge and figures stand above the ground: a click on them is a click on the unit's hex, not on the
+ // terrain behind them (owner's report on v0.2.0). Not for the keyboard's look-at point (pills false).
+ var units = pills && cam != null ? Map.Terrain.transform.Find(Units.UnitLayer.RootName) : null;
+ if (units != null && units.TryGetComponent<Units.UnitLayer>(out var layer) && layer.TokenAtScreen(cam!, pos) is Units.PlacedToken token)
+ return (token.Q, token.R);
if (cam == null || !TryGroundHit(Map, cam.ScreenPointToRay(pos), out var hit)) return null;
return Pick(Map, hit.point);
}
diff --git a/unity/Assets/Scripts/Map/Units/UnitLayer.cs b/unity/Assets/Scripts/Map/Units/UnitLayer.cs
index 2d06855d0..d54d91016 100644
--- a/unity/Assets/Scripts/Map/Units/UnitLayer.cs
+++ b/unity/Assets/Scripts/Map/Units/UnitLayer.cs
@@ -323,6 +323,33 @@ namespace Statecraft.Game.Map.Units
return null;
}
+ /// <summary>The token <paramref name="cam"/> draws under <paramref name="screen"/> (pixels, y up): its badge's or its
+ /// standing pieces' box (<see cref="KeepClear"/>) as that camera projects it, the nearest to the camera when several
+ /// hold the point; null off every token, behind the camera, or while the "units" toggle hides the layer. The badge
+ /// floats over the figures and behind them, so the terrain under it is often the next hex (owner's report on v0.2.0:
+ /// a click on a unit's badge showed the hex behind it, with no Move). No allocation.</summary>
+ public PlacedToken? TokenAtScreen(Camera cam, Vector2 screen)
+ {
+ if (cam == null || !gameObject.activeInHierarchy) return null;
+ FaceCamera(cam); // the boxes as this camera draws them (a no-op from the pose it last turned them to)
+ PlacedToken? best = null; float nearest = float.MaxValue;
+ for (int i = 0; i < placed.Count && i < keepClear.Count; i++)
+ foreach (var box in keepClear[i])
+ {
+ float x0 = float.MaxValue, y0 = float.MaxValue, x1 = float.MinValue, y1 = float.MinValue; bool behind = false;
+ for (int c = 0; c < 8 && !behind; c++)
+ {
+ var p = cam.WorldToScreenPoint(new Vector3((c & 1) == 0 ? box.min.x : box.max.x, (c & 2) == 0 ? box.min.y : box.max.y, (c & 4) == 0 ? box.min.z : box.max.z));
+ behind = p.z <= 0f;
+ x0 = Mathf.Min(x0, p.x); y0 = Mathf.Min(y0, p.y); x1 = Mathf.Max(x1, p.x); y1 = Mathf.Max(y1, p.y);
+ }
+ if (behind || screen.x < x0 || screen.x > x1 || screen.y < y0 || screen.y > y1) continue;
+ float depth = (box.center - cam.transform.position).sqrMagnitude;
+ if (depth < nearest) { nearest = depth; best = placed[i]; }
+ }
+ return best;
+ }
+
/// <summary>Selects a drawn unit by id (the ring goes round it), or clears the selection (null).</summary>
public void Select(string? id)
{
diff --git a/unity/Assets/Tests/EditMode/UnitBadgeClickTests.cs b/unity/Assets/Tests/EditMode/UnitBadgeClickTests.cs
new file mode 100644
index 000000000..779ef1409
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/UnitBadgeClickTests.cs
@@ -0,0 +1,69 @@
+#nullable enable
+using System.IO;
+using System.Linq;
+using NUnit.Framework;
+using Statecraft.Game.Map;
+using Statecraft.Game.Map.Units;
+using UnityEditor.SceneManagement;
+using UnityEngine;
+
+/// <summary>Owner's report on v0.2.0 (22 October 2026): "unit movement is not working, nothing happens". A unit's badge
+/// floats over its token "so a player sees it is a unit and can click it" (UnitBadge), but a click there picks the hex of the
+/// terrain behind it (MapPointer.HexAt raycasts the terrain only), so the card shows that hex, with no Move.</summary>
+public class UnitBadgeClickTests
+{
+ MapScene map = null!;
+ Camera cam = null!;
+ CameraRig rig = null!;
+ RenderTexture rt = null!;
+ GameObject pointerGo = null!;
+
+ [SetUp]
+ public void Open()
+ {
+ EditorSceneManager.OpenScene("Assets/Map.unity", OpenSceneMode.Single);
+ map = Object.FindFirstObjectByType<MapScene>(); map.Build();
+ cam = Camera.main; rig = cam.GetComponent<CameraRig>();
+ rt = new RenderTexture(1920, 1080, 24); cam.targetTexture = rt; // the browser's 1920x1080 at the camera's pixels
+ pointerGo = new GameObject("BadgeClickPointer");
+ }
+
+ [TearDown]
+ public void Close()
+ {
+ cam.targetTexture = null; Object.DestroyImmediate(rt); Object.DestroyImmediate(pointerGo);
+ EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
+ }
+
+ /// <summary>With the camera over each Israeli brigade in turn (the rig's default 90 km, yaw 0, its fixed 50 degree pitch), a
+ /// click on the brigade's badge picks the brigade's hex, as a click on its figures' base does: the hex
+ /// GameRoot.OnHexClicked turns into the unit's card and its Move.</summary>
+ [Test]
+ public void AClickOnAUnitsBadgePicksTheUnitsHex()
+ {
+ var content = Statecraft.Sim.ContentLoader.Load(Statecraft.Game.Paths.Content);
+ var units = UnitData.Parse(File.ReadAllText(Path.Combine(Statecraft.Game.Paths.Content, "map", UnitData.DefaultFileName)));
+ var layer = UnitLayer.Create(map, units, content.Hexes, still: true);
+ layer.Show(content, Statecraft.Sim.Simulation.NewGame(content, Statecraft.Sim.GoldenRun.Seed, Statecraft.Sim.Mode.Historical));
+ var pointer = pointerGo.AddComponent<MapPointer>();
+ pointer.Map = map;
+
+ var tokens = GameObject.Find(UnitLayer.RootName).transform.Find("Tokens");
+ int clicked = 0;
+ var misses = new System.Collections.Generic.List<string>();
+ foreach (var t in layer.Tokens.Where(t => t.Token.Unit.Side == UnitData.Israel && t.Token.Unit.Service == UnitData.Army))
+ {
+ var badge = tokens.Find(t.Token.Unit.Id).Find("Badge");
+ rig.LookAt = new Vector3(badge.position.x, 0f, badge.position.z); rig.Distance = 90000f; rig.Yaw = 0f; rig.TopInset = 0; rig.Apply();
+ layer.FaceCamera(cam); // the badges where this camera draws them
+ var s = cam.WorldToScreenPoint(badge.position); // pixels, y up: what MapPointer gets from the mouse
+ if (s.z <= 0 || s.x < 0 || s.y < 0 || s.x > cam.pixelWidth || s.y > cam.pixelHeight) continue; // off screen
+ clicked++;
+ var hex = pointer.HexAt(new Vector2(s.x, s.y));
+ if (hex != (t.Q, t.R)) misses.Add($"{t.Token.Unit.Id} on {t.Q},{t.R}: its badge at ({s.x:0}, {s.y:0}) picks {hex}");
+ }
+ TestContext.WriteLine($"{clicked} badges on screen; misses:\n" + string.Join("\n", misses));
+ Assert.Greater(clicked, 2, "the brigades are drawn");
+ Assert.IsEmpty(misses, "a click on a unit's badge must pick the unit's hex");
+ }
+}
diff --git a/unity/Assets/Tests/EditMode/UnitBadgeClickTests.cs.meta b/unity/Assets/Tests/EditMode/UnitBadgeClickTests.cs.meta
new file mode 100644
index 000000000..47a7bd427
--- /dev/null
+++ b/unity/Assets/Tests/EditMode/UnitBadgeClickTests.cs.meta
@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: caeaf8b786c5c4161a8522153455ac85
\ No newline at end of file