Hello, I’m making a game with chess like feel but with movements with a radius limit. The first thing to know is that a piece is called a unit, and they have a limited radius on where they can move. And also their positions should be without decimals. These units have a size of a regular square, they have a default radius of 2, and when pressed would spawn tiles to show the player where it is allowed to move:
But when the unit has a border or boundary (that has collision) inside it’s intended movement radius, it still is a possible movement square, and worse of all, it can overlap:
I’m trying a bunch of overly complicated ways like sweep raycasting but they are hard to do.
Here is the Possible movement code:
void Place(Vector3 pos)
{
MovableSprite sprite = selectedSprite;
if (hasSelected)
{
if (sprite != null)
{
float x = Mathf.Abs(pos.x - (int)sprite.transform.position.x) * (1);
float y = Mathf.Abs(pos.y - (int)sprite.transform.position.y) * (1);
float net = x + y;
//selectedMinLimit = minRad;
MeasureLimits(sprite, pos);
if (net > sprite.MaxRadius)
{
Debug.LogWarning($"Object {sprite.gameObject.name} is beyond its limits");
Debug.LogWarning(sprite.gameObject.name + " has not been placed");
}
else
{
sprite.Move(pos);
Debug.Log(sprite.gameObject.name + " has been placed");
}
selectedSprite = null;
hasSelected = false;
sprite.SelectUI(false);
StartCoroutine(PlacePossibleTiles(false, sprite));
}
}
}
And here is the Placement of the Possible Movement Tiles
IEnumerator PlacePossibleTiles(bool place, MovableSprite sprite)
{
if (place)
{
if (sprite != null)
{
int x = sprite.MaxRadius * -1;
int y = sprite.MaxRadius * -1;
GeneratingSide side = GeneratingSide.BL;
bool done = false;
while (!done)
{
if (y > sprite.MaxRadius)
{
done = true;
}
side = MathUtil.GetGeneratingSide(x, y);
if ((Mathf.Abs(x) + Mathf.Abs(y)) > sprite.MaxRadius)
{
}
else
{
Vector3Int poos = new Vector3Int((int)sprite.transform.position.x - 1 + x,
(int)sprite.transform.position.y - 1 + y);
possiblePlacesTilemap.SetTile(poos, possibleTile);
}
x++;
if (x > sprite.MaxRadius)
{
x = sprite.MaxRadius * -1;
y++;
}
}
}
}
else
{
possiblePlacesTilemap.ClearAllTiles();
}
yield return null;
}
If anyone has an answer, let me know, Thanks.