Hello,
I’m creating an isometric game and I used astar pathfinding and I use sprite with 8 directions, as with astar pathfinding to make my sprite change to the correct direction in which it goes after?
You can compare where your sprite is this frame with the previous frame, if you keep its previous position.
Vector3 previousPosition;
In your Update() method:
Vector3 delta = transform.position - previousPosition;
previousPosition = transform.position;
Now every frame delta will have how much you moved this frame.
From that, you can decide one of eight directions with if/else statements:
const float thresshold = 0.01f; // play with this value
if (delta.x > threshhold) // must be going right
{
if (delta.y > 0)
{
// up / right
}
else
{
if (delta.y < 0)
{
// down / right
}
else
{
// pure right
}
}
else
{
/// I'll let you write the part for left!
}