From a4badc276b0ae421771d824bca6df5b5d05c4d81 Mon Sep 17 00:00:00 2001 From: dingo Date: Wed, 1 Apr 2026 20:30:39 -0400 Subject: [PATCH] Unit models face nearest enemy rather than fixed north/south _idle_rotation now: - Finds the nearest enemy body in _unit_bodies by XZ distance - Returns atan2 toward that position minus the model's base angle - Falls back to the original north/south heuristic when no enemy exists yet Also refreshes idle rotation for all units on every state rebuild (step 4 in _rebuild_units), skipping any unit currently in a move tween, so rotations stay correct as units reposition across turns. Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude Co-Authored-By: Happy --- .../origin-client/scenes/game/game_screen.gd | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/Client/origin-client/scenes/game/game_screen.gd b/Client/origin-client/scenes/game/game_screen.gd index 9d74c04..93dfbdb 100644 --- a/Client/origin-client/scenes/game/game_screen.gd +++ b/Client/origin-client/scenes/game/game_screen.gd @@ -1484,6 +1484,13 @@ func _rebuild_units(state: Dictionary) -> void: var d := desired[iid] as Dictionary _add_unit_mesh(d["unit"] as Dictionary, d["color"] as Color, d["is_enemy"] as bool) + # 4. Refresh idle rotation for all units — nearest enemy may have changed. + for iid : String in _unit_meshes: + if _move_anims.has(iid): continue # don't interrupt an in-progress move + var m := _unit_meshes.get(iid) as Node3D + if is_instance_valid(m): + m.rotation.y = _idle_rotation(iid) + func _add_unit_mesh(unit: Dictionary, team_color: Color, is_enemy: bool = false) -> void: var pd : Dictionary = unit.get("position", {}) @@ -1789,13 +1796,31 @@ func _facing_to_base_angle(facing: String) -> float: "west": return -PI / 2.0 _: return PI # "south" / default / null — model faces +Z (Blender GLB) -## Returns the rotation.y (radians) that makes a unit's model face its opponent -## across the board, given its stored base angle and team-flip flag. +## Returns the rotation.y (radians) that makes a unit's model face the nearest +## enemy unit on the board. Falls back to the fixed north/south heuristic when +## no enemy body is found (e.g. during initial placement before enemies exist). func _idle_rotation(iid: String) -> float: var base : float = float(_model_base_angles.get(iid, 0.0)) - var flip : bool = bool(_needs_face_rot.get(iid, false)) - # flip=true → unit should face +Z (south, toward P2) atan2(0,-1)=π - # flip=false → unit should face -Z (north, toward P1) atan2(0, 1)=0 + var my_body := _unit_bodies.get(iid) as StaticBody3D + if is_instance_valid(my_body): + var my_pos : Vector3 = my_body.global_position + var best_sq : float = INF + var best_dir : Vector3 = Vector3.ZERO + var enemy_ids : Array = _enemy_unit_ids.keys() if _is_enemy_unit(iid) == false \ + else _friendly_unit_ids.keys() + for eid in enemy_ids: + var eb := _unit_bodies.get(eid) as StaticBody3D + if not is_instance_valid(eb): continue + var d : Vector3 = eb.global_position - my_pos + var sq : float = d.x * d.x + d.z * d.z + if sq < best_sq: + best_sq = sq + best_dir = d + if best_sq < INF and best_dir.length_squared() > 0.001: + var flat := Vector3(best_dir.x, 0.0, best_dir.z).normalized() + return atan2(flat.x, -flat.z) - base + # Fallback: fixed north/south heuristic. + var flip : bool = bool(_needs_face_rot.get(iid, false)) var dir := Vector3(0, 0, 1) if flip else Vector3(0, 0, -1) return atan2(dir.x, -dir.z) - base