Hey guys,
For the past week or so I’ve been spending my spare time trying different methods to create a “fog of war” type effect for a RTS type game.
I have found numerous posts on here with plentyful information, and through them have found the most optimised method out of the ones suggested.
Notes:
-I do not have Unity Pro (yet) and would much prefer to build this feature on my own (please no suggestions that require Unity Pro, or someone elses work from the Asset Store).
-The game I am working on will require a vast amount of units to be on the screen at any given time. Hence the performance issues.
So, the most optimised method that I have found out of many that I have so far tried is to place a single plane just above the walkable area and raycast onto the plane. Then iterate through each vertex of the plane and make each vertice within range have an alpha of 0 in color, ultimately creating a transparent circle(ish) around the unit.
This works really well visually, The plane that I am currently using has 1089 vertice in total. I could potentially shave a some vertices off of the plane however making it any smaller would cause unpreferrable accuracy due to the space between the vertices.
The major issue is that although I am currently working on the game entirely using the basic geometry that comes with Unity (I haven’t yet built any models to use in the game), after spawning roughly 5 units, each raycasting onto the plane every 0.5 seconds, the fps drops way too much. Particularly considering that the game would ideally need to support far more units than this.
Here is the basis of my code:
void calculateVision()
{
//because the plane's normal is facing up the way, we our ray to collide from above, downwards.
ray = new Ray(transform.position+(transform.up*5f),-transform.up);
if(cnfg.fogcollider.Raycast(ray,out hitinfo, 5f))
{
//get the relative position of the hit point on the fogplane's mesh
relPoint = cnfg.fogmesh.transform.InverseTransformPoint(hitinfo.point);
Color[] colors = cnfg.fogmesh.mesh.colors;
//iterate through each vertice
for(int i = 0; i < cnfg.fogmesh.mesh.vertices.Length; i++)
{
//if the distance between the vertice and the hit point is less than vision range, grant vision
if(Vector3.Distance(cnfg.fogmesh.mesh.vertices*, relPoint) < visionRange)*
-
{*
_ colors*.a = 0f;_
_ }_
_ }*_
* //apply the changes*
* cnfg.fogmesh.mesh.colors = colors;*
* }*
* //rerun the function*
* Invoke(“calculateVision”,cnfg.minionFogIteration);*
* }*
cnfg.fogmesh is the meshFilter of the fog plane.
So ultimately my question is, other than further reducing the cnfg.minionFogIteration time and lowering the vertex count in the fog plane, is there a more optimised method to achieve what it is that I am trying to do, or am I just plain stupid for trying to do this in Unity Free?
I will be happy to provide a package of what I have so far if that would be any use to anyone.
Many thanks in advance for any suggestions.