Skip to content
Snippets Groups Projects
TerrainLooker.cs 1008 B
Newer Older
Chang, Ryan's avatar
Chang, Ryan committed
using System.Collections;
using UnityEngine;

public class TerrainLooker : MonoBehaviour
{
    [Header("User settings")]
    public float gridSize = 1000;

    [Header("Autogenerated values")]
    public Transform player;

    public TerrainGenerator terrainGenerator;

    private void Start()
    {
        GameObject po = GameObject.FindGameObjectWithTag("Player");
        player = po.transform;

        gameObject.RequireComponentAuto(out terrainGenerator, "terrain generator");
    }

    private void Update()
    {
        float hgs = gridSize / 2;
        Vector3 ppos = player.position;
        Vector2Int pGridPos = new(Mathf.RoundToInt(ppos.x / hgs), Mathf.RoundToInt(ppos.y / hgs));
        Vector2Int cGridPos = terrainGenerator.currentGridPosition;

        if (pGridPos != cGridPos)
        {
            terrainGenerator.currentGridPosition = pGridPos;
Chang, Ryan's avatar
Chang, Ryan committed
            terrainGenerator.GenerateSurronding(terrainGenerator.crawlStartPosition + (Vector2)pGridPos * gridSize);
Chang, Ryan's avatar
Chang, Ryan committed
        }
    }
}