Make a soldier or turret turn the short way round, at a fixed speed
Tell a unit to face a target and sometimes it spins almost all the way round the long way, or twitches at the moment it arrives. This entry explains why angles on a circle trick simple code, and gives a turning step that is smooth, never overshoots and handles a target straight behind.
Why this works
A direction is a place on a circle, and the circle has a seam where the numbers jump back to the start. Plain subtraction ignores that seam. Measuring the gap the short way round, and never turning further than the gap, gives a unit that always lands exactly on target.
When not to use it
Skip it when the turn itself is part of the game, such as a wheel the player spins by hand or a dial that must be wound round several times. There the long way and the count of full turns matter, so keep a running total instead.
In the full entry
- What to build, step by step
- Starting values and the reason for each
- 4 common mistakes and the fix
- 19 lines of example code with 4 checks that pass
- How your AI shows you that it works
Say this to your AI
Use the Spielerando entry "Make a soldier or turret turn the short way round, at a fixed speed" (read_entry unit-turns-the-short-way-round-without-spinning) in my game. Build it step by step, and show me how you checked that it works.Or skip this and connect the MCP: your AI then finds all of this by itself.Connect the MCP