- Home /
Having Trouble With 2D Movement
Can anybody help me, I have 2 methods, 1 that highlights all tiles you can move to and another that moves you to it. I use the same code to decide whether a tile should be highlighted and if I can move to it, however, I'm not able to move to all the highlighted tiles. Can anybody help?
public void Move(int playerSpeed)
{
mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
Vector3Int cellPos = grid.WorldToCell(mousePos);
if (Mathf.Abs(Vector3.Distance(cellPos, player.transform.position)) <= playerSpeed)
{
moveableAreas.SetTileFlags(cellPos, TileFlags.None);
moveableAreas.SetColor(cellPos, Color.black);
player.transform.position = grid.GetCellCenterWorld(cellPos);
foreach (Vector3 refr in tileRefs)
{
Vector3Int cellPos2 = grid.WorldToCell(refr);
moveableAreas.SetTileFlags(cellPos2, TileFlags.None);
moveableAreas.SetColor(cellPos2, Color.white);
}
}
}
public void HighlightMoveable(GameObject player, int playerSpeed)
{
foreach(Vector3 refr in tileRefs)
{
if (Mathf.Abs(Vector3.Distance(refr, player.transform.position)) <= playerSpeed)
{
Vector3Int cellPos = grid.WorldToCell(refr);
moveableAreas.SetTileFlags(cellPos, TileFlags.None);
moveableAreas.SetColor(cellPos, Color.red);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity TextMeshPro UGUI object is hidden behind other sprites 0 Answers