// Update is called once per frame
    void Update()
    {
        timer -= Time.deltaTime;

        int previousChunkx = Mathf.FloorToInt(previousPosition.x / tilex);
        int previousChunky = Mathf.FloorToInt(previousPosition.y / tiley);

        int currentChunkx = Mathf.FloorToInt(playerTransform.position.x / tilex);
        int currentChunky = Mathf.FloorToInt(playerTransform.position.y / tiley);

        previousPosition = playerTransform.position;

        if (previousChunkx == currentChunkx && previousChunky == currentChunky)           // C --> C
        {
            return;
        }

        if (previousChunkx + 1 == currentChunkx && previousChunky + 1 == currentChunky)           // C --> NE
        {
            W.Unload();
            SW.Unload();
            S.Unload();
            NW.Unload();
            SE.Unload();
            W  = N;
            SW = C;
            S  = E;
            C  = NE;
            NW = GenChunk(currentChunkx - 1, currentChunky + 1);
            N  = GenChunk(currentChunkx, currentChunky + 1);
            NE = GenChunk(currentChunkx + 1, currentChunky + 1);
            E  = GenChunk(currentChunkx + 1, currentChunky);
            SE = GenChunk(currentChunkx + 1, currentChunky - 1);
        }

        if (previousChunkx == currentChunkx && previousChunky + 1 == currentChunky)           // C --> N
        {
            SW.Unload();
            S.Unload();
            SE.Unload();
            SW = W;
            S  = C;
            SE = E;
            W  = NW;
            C  = N;
            E  = NE;
            NW = GenChunk(currentChunkx - 1, currentChunky + 1);
            N  = GenChunk(currentChunkx, currentChunky + 1);
            NE = GenChunk(currentChunkx + 1, currentChunky + 1);
        }

        if (previousChunkx - 1 == currentChunkx && previousChunky + 1 == currentChunky)           // C --> NW
        {
            S.Unload();
            SE.Unload();
            E.Unload();
            NE.Unload();
            SW.Unload();
            E  = N;
            SE = C;
            S  = W;
            C  = NW;
            NW = GenChunk(currentChunkx - 1, currentChunky + 1);
            N  = GenChunk(currentChunkx, currentChunky + 1);
            NE = GenChunk(currentChunkx + 1, currentChunky + 1);
            W  = GenChunk(currentChunkx - 1, currentChunky);
            SW = GenChunk(currentChunkx - 1, currentChunky - 1);
        }

        if (previousChunkx + 1 == currentChunkx && previousChunky == currentChunky)           // C --> E
        {
            NW.Unload();
            W.Unload();
            SW.Unload();
            NW = N;
            W  = C;
            SW = S;
            N  = NE;
            C  = E;
            S  = SE;
            NE = GenChunk(currentChunkx + 1, currentChunky + 1);
            E  = GenChunk(currentChunkx + 1, currentChunky);
            SE = GenChunk(currentChunkx + 1, currentChunky - 1);
        }

        if (previousChunkx - 1 == currentChunkx && previousChunky == currentChunky)           // C --> W
        {
            NE.Unload();
            E.Unload();
            SE.Unload();
            NE = N;
            E  = C;
            SE = S;
            N  = NW;
            C  = W;
            S  = SW;
            NW = GenChunk(currentChunkx - 1, currentChunky + 1);
            W  = GenChunk(currentChunkx - 1, currentChunky);
            SW = GenChunk(currentChunkx - 1, currentChunky - 1);
        }

        if (previousChunkx + 1 == currentChunkx && previousChunky - 1 == currentChunky)           // C --> SE
        {
            W.Unload();
            NW.Unload();
            N.Unload();
            NE.Unload();
            SW.Unload();
            NW = C;
            N  = E;
            C  = SE;
            W  = S;
            NE = GenChunk(currentChunkx + 1, currentChunky + 1);
            E  = GenChunk(currentChunkx + 1, currentChunky);
            SE = GenChunk(currentChunkx + 1, currentChunky - 1);
            S  = GenChunk(currentChunkx, currentChunky - 1);
            SW = GenChunk(currentChunkx - 1, currentChunky - 1);
        }

        if (previousChunkx == currentChunkx && previousChunky - 1 == currentChunky)           // C --> S
        {
            NW.Unload();
            N.Unload();
            NE.Unload();
            NW = W;
            N  = C;
            NE = E;
            W  = SW;
            C  = S;
            E  = SE;
            SW = GenChunk(currentChunkx - 1, currentChunky - 1);
            S  = GenChunk(currentChunkx, currentChunky - 1);
            SE = GenChunk(currentChunkx + 1, currentChunky - 1);
        }

        if (previousChunkx - 1 == currentChunkx && previousChunky - 1 == currentChunky)           // C --> SW
        {
            NW.Unload();
            N.Unload();
            NE.Unload();
            E.Unload();
            SE.Unload();
            N  = W;
            NE = C;
            E  = S;
            C  = SW;
            W  = GenChunk(currentChunkx - 1, currentChunky);
            SW = GenChunk(currentChunkx - 1, currentChunky - 1);
            S  = GenChunk(currentChunkx, currentChunky - 1);
            NW = GenChunk(currentChunkx - 1, currentChunky + 1);
            SE = GenChunk(currentChunkx + 1, currentChunky - 1);
        }

        if (C.Horse)
        {
            C.Horse.GetComponent <FreeHorseAI> ().Activate(playerTransform);
            ShowNextCredits();
        }
    }