Checking if enemy is on fog tile

So what I’m attempting to do is tell if an enemy is on a certain tile that either has fog active or not active. If it’s active it changes to an alpha texture and if not then it has it’s basic texture. I’ve tried using triggers but having about 2000 triggers in one scene causes my fps to drop too low. I need a simple way to check if my enemy is standing on different tiles. Is raycasting at the tile prefab and checking the script attached to see if it’s a fog tile or non fog tile a possibility? I’m kind of stumped.

There are many ways of solving this problem, but alot depends on how you organize the tiles. With your description, you could have one script that managed all the trigger-colliders. If the tiles were referenced in an array, then just check the players position in relation to the array.

I have shown 2 raycasting methods. For this to work, it needs scripts to be added to the tiles and the player.

method 1- requires 2 tags to be created. Add tags ‘Clear’ and ‘Foggy’

method 2- this is NOT an advisable method, but illustrates the process with a raycast. If you were raycasting from a single event (say a mouse-click or trigger enter) then this would be fine.

Both methods are in the following scripts.

Player script :

#pragma strict

private var debugString : String = "";
private var debugInt : int = 0;

function Update() 
{
    var hit : RaycastHit;
    if ( Physics.Raycast (transform.position, Vector3.down, hit, 10) ) 
    {
        // using the collider's tag method
        if (hit.collider.gameObject.tag == "Foggy")
        {
            debugString = "FOGGY TILE !";
        }
        else
        {
            debugString = ("Standing on " + hit.collider.gameObject.tag + " tile");
        }

        // using GetComponent method - *Note : Tiles is the name of the script on all the tiles
        var tileScript : Tiles = hit.collider.transform.gameObject.GetComponent("Tiles") as Tiles;
        debugInt = tileScript.tileState; // reading a variable from off the other script
    }
}

function OnGUI() 
{
    GUI.Box( Rect( 50, 10, Screen.width - 100, 25 ), debugString );
    GUI.Box( Rect( 50, 35, Screen.width - 100, 25 ), "GetComponent value = " + debugInt.ToString() );
}

Tile script :

#pragma strict

public var matClear : Material;
public var matFoggy : Material;
public var tileState : int;

function Start() 
{
    tileState = 0;
    this.transform.renderer.material = matClear;

    InvokeRepeating("ChangeType", 0.5, 3.5);
}

function ChangeType()
{
    var rnd : int = Random.Range(0, 2);

    switch(rnd)
    {
        case 0 :
            tileState = 0;
            this.transform.renderer.material = matClear;
            this.transform.gameObject.tag = "Clear";
        break;
        case 1 :
            tileState = 1;
            this.transform.renderer.material = matFoggy;
            this.transform.gameObject.tag = "Foggy";
        break;
    }
}