Hi
I´m working on a turn based strategy game. Right now I display the area a character can move using projectors (for terrain adaptability of the tiles). Every tile is an empty with a projector attached and every projector within the movement area is being turned on to display a single tile (see screenshot). You can imagine that this is really unperformant (I read something about that a mesh has to be rendered again for every projector it receives).
I wanted to just make an outline (by using a line renderer for example) of the area where a character can move (similar to XCOM 2) have no idea how to do that. Can anyone help? The area is stored within an array of transforms.
If it somehow helps here’s the code which generates the area:
using UnityEngine;
using System.Collections;
namespace Grid
{
[System.Serializable]
public class AreaBuilder: MonoBehaviour, AreaBuilderInterface
{
public Grid grid;
public ArrayList traversed;
public LayerMask layerMask;
public void buildArea(Vector3 start, int min, int max)
{
GameObject startTile = this.grid.getTileAtPosition(start.x, start.z);
ArrayList openList = new ArrayList();
openList.Add(startTile);
this.traversed = new ArrayList();
ArrayList withinMinArea = new ArrayList();
this.traverseTiles(openList, withinMinArea, min, max, true);
if(this.traversed != null)
{
for (int i = 0; i < this.traversed.Count; i++)
{
GameObject currentCell = (GameObject)this.traversed*;*
currentCell.GetComponent().enabled = true;
}
}
}
private void traverseTiles(ArrayList openList, ArrayList withinMinArea, int min, int max, bool useRaycast)
{
if(max == 0)
{
return;
} else
{
ArrayList tempOpenList = new ArrayList();
for (int i = 0; i < openList.Count; i++)
{
GameObject currentTile = (GameObject) openList*;*
Tile.AbstractTileController projectorTile = currentTile.GetComponent<Tile.AbstractTileController>();
for (int j = 0; j < projectorTile.neighbors.Count; j++)
{
GameObject currentNeighbor = (GameObject) projectorTile.neighbors[j];
if (currentNeighbor == null)
{
continue;
}
if (useRaycast)
{
RaycastHit hit;
Vector3 currentCorrectedPosition = new Vector3(currentTile.transform.position.x,
currentTile.transform.position.y + 3f, currentTile.transform.position.z);
Vector3 neighborCorrectedPosition = new Vector3(currentNeighbor.transform.position.x,
currentTile.transform.position.y + 3f, currentNeighbor.transform.position.z);
Debug.DrawLine(currentCorrectedPosition, neighborCorrectedPosition, Color.red, 60);
Vector3 checkDirection = neighborCorrectedPosition - currentCorrectedPosition;
int ignoreLayer = ~(1 << 9);
Ray ray = new Ray();
ray.origin = currentCorrectedPosition;
ray.direction = checkDirection;
if (!Physics.Raycast(ray, out hit, Vector3.Distance(currentCorrectedPosition, neighborCorrectedPosition), this.layerMask))
{
if (!this.traversed.Contains(currentNeighbor))
{
this.traversed.Add(currentNeighbor);
}
if (!withinMinArea.Contains(currentNeighbor) && min < max)
{
withinMinArea.Add(currentNeighbor);
}
if (!tempOpenList.Contains(currentNeighbor))
{
tempOpenList.Add(currentNeighbor);
}
}
}
}
}
max–;
this.traverseTiles(tempOpenList, withinMinArea, min, max, useRaycast);
}
}
public void dropArea()
{
foreach(GameObject tile in this.traversed)
{
tile.GetComponent().enabled = false;
}
}
}
}
