Hey everybody,
I tried implementing a character direction change based on the path the character is moving on. As I am using a Pathfinder algorithm it happens that he needs to change twice direction during the same path (L shape path) in the case it would mean changing position once to DOWN and at the corner to RIGHT
the Code shown below works for a single direction change, any idea on how to make it recognize a second one ?
private void MoveAlongPath()
{
var step = speed * Time.deltaTime;
float zIndex = path[0].transform.position.z;
character.transform.position = Vector2.MoveTowards(character.transform.position, path[0].transform.position, step);
character.transform.position = new Vector3(character.transform.position.x, character.transform.position.y, zIndex);
for (int i = 0; i < path.Count; i++)
{
var previousTile = i > 0 ? path[i - 1] : character.standingOnTile;
var futureTile = i < path.Count - 1 ? path[i + 1] : null;
var currentTile = path;
Debug.Log("currenttile : " + previousTile.grid2DLocation);
if (currentTile.transform.position.x > previousTile.transform.position.x && currentTile.transform.position.y > previousTile.transform.position.y)
{
character.SetDirection(Vector2.right);
}
else if (currentTile.transform.position.x < previousTile.transform.position.x && currentTile.transform.position.y < previousTile.transform.position.y)
{
character.SetDirection(Vector2.left);
}
else if (currentTile.transform.position.x > previousTile.transform.position.x && currentTile.transform.position.y < previousTile.transform.position.y)
{
character.SetDirection(Vector2.down);
}
else if (currentTile.transform.position.x < previousTile.transform.position.x && currentTile.transform.position.y > previousTile.transform.position.y)
{
character.SetDirection(Vector2.up);
}
}
if (Vector2.Distance(character.transform.position, path[0].transform.position) < 0.00001f)
{
PositionCharacterOnLine(path[0]);
path.RemoveAt(0);
}
if (path.Count == 0)
{
GetInRangeTiles(false);
isMoving = false;
}
}