Change player sprite based on relative mouse position

Hello!
I am new to Defold and have very little experience with programming. I’m trying to make isometric shooter.
What would be the best solution to change player sprite (8-dir) based on mouse position relative to player?

I would recommend using 3D models and rotating them. It will save a lot of animation work in the long run. I was using sprites in my game initially but then the camera perspective becomes fixed (if you change things even a bit the 3D effect breaks) and you have to draw the sprites (or render them to sprites from a 3D model) in different angles. You can also apply post processing effects to emulate the 2D sprite effect if you want that way.

1 Like

The first thing that comes to mind is:

  1. Get cursor position in world space.
  2. Subtract character pos from cursor pos to get relative vector.
  3. Use math.atan2 to get the vector’s angle.
  4. Round/snap that angle to be one of the 8 directions.
  5. Use a table with angle keys and image/animation-name values to select the right image for your sprite.

There’s probably some fiddly-ness with step 4. You can google something like, “snap to 8 directions” to find more detailed info about it.

2 Likes

@Epitaph64 thank you for your answer. I’m not that great with 3d models, so I lean to pixel-art.

@ross.grams thanks! I had something like that in mind, exept for atan2.

1 Like