- Home /
Other
2D Isometric Movement Path (8 Directions)
Hi there,
I'm developing a 2d isometric game and the character must move in 8 directions, i have created animations but the character can't find the right animation while he's going to the target!
public void AngleToVectorDirection(Vector2 dir)
{
Vector2 normDir = dir.normalized;
//calculate how many degrees one slice is
float step = 360f / 8;
//calculate how many degress half a slice is.
//we need this to offset the pie, so that the North (UP) slice is aligned in the center
float halfstep = step / 2;
//get the angle from -180 to 180 of the direction vector relative to the Up vector.
//this will return the angle between dir and North.
//
float angle = Mathf.Atan2(dir.x, dir.y) * Mathf.Rad2Deg;
float x = dir.x;
float y = dir.y;
if (angle >= 157.5 && angle < 202.5)
{
animator.Play("RunRight");
return;
}
else if (angle >= 112.5 && angle < 157.5)
{
animator.Play("RunUpRight");
return;
}
else if (angle >= 67.5 && angle < 112.5)
{
animator.Play("RunUp");
return;
}
else if (angle >= 22.5 && angle < 67.5)
{
animator.Play("RunUpLeft");
return;
}
else if (angle >= 337.5 || angle < 22.5)
{
animator.Play("RunLeft");
return;
}
else if (angle >= 292.5 && angle < 337.5)
{
animator.Play("RunDownLeft");
return;
}
else if (angle >= 247.5 && angle < 292.5)
{
animator.Play("RunDown");
return;
}
else if (angle >= 202.5 && angle < 247.5)
{
animator.Play("RunDownRight");
return;
}
}
Answer by Bunny83 · Jan 03, 2020 at 10:34 AM
Is it possible that you just copy and pasted all that code together somehow? Because have you actually read the comment before the "Atan2" line? "angle" will be in the range -180 to 180. So many of your if statements can never be reached since you have values greater than 180 as minimum condition. You probably wanted to "shift" the angle from the range [-180, 180]
to [0, 360]
. To do that you have to add 180 to the angle.
Note you want to add an f
suffix to all your number constants in your if statements. Otherwise you force an implicit type conversion of angle from float to double in every if statement. I'm also wondering since you calculated your "halfstep" (22.5), why have you used hardcoded constants?
Currently you have 8 if statements. In the worst case you have to check all 8 every frame. It's usually simpler to just work with the normalized direction vector and doing a 4 step nested if - else chain. That way the overhead stays constant and you don't need to calculate atan2. However you can of course stick to your angle based approach. Though currently you check every limit twice which is not necessary in an else-if chain. All you have to do is order your statements correctly. You also do not need the return statements since an else-if chain is broken automatically once it enters one statement.
float angle = 180f + Mathf.Atan2(dir.x, dir.y) * Mathf.Rad2Deg;
if (angle > 337.5f || angle < 22.5f)
{
animator.Play("RunLeft");
}
else if (angle > 292.5f)
{
animator.Play("RunDownLeft");
}
else if (angle > 247.5f)
{
animator.Play("RunDown");
}
else if (angle > 202.5f)
{
animator.Play("RunDownRight");
}
if (angle > 157.5f)
{
animator.Play("RunRight");
}
else if (angle > 112.5f)
{
animator.Play("RunUpRight");
}
else if (angle > 67.5f)
{
animator.Play("RunUp");
}
else
{
animator.Play("RunUpLeft");
}
Answer by Ardaze · Jan 03, 2020 at 12:43 PM
This is how i set animations and values.
Check the Image: https://ibb.co/8MY9G4k
Follow this Question
Related Questions
2D Pathfinding Top Down 0 Answers
Move towards move very fast independent of the speed 1 Answer
How to highlight only walkable path on tilemap,Sorting walkable path on a tilemap 0 Answers
2D C# isometric Tile-based movement and collision problem 1 Answer
Isometric - Camera follow an object when at the edge of the screen 1 Answer