Home of Ebyan Alvarez-Buylla

Cardinal Movement Relative to Player Screen Position

Cardinal Movement Relative to Player Screen Position

Here’s something you’re likely to have run across: the player character is in the center of the screen, and you want it to move in the cardinal direction you click, relative to its position—clicking above it moves north, below south, to the right east, and to the left west.

I’ve implemented this control scheme in my current project, settling on the following technique after a less-than-elegant implementation relying heavily on if-elses: