So I am making in unity game that is a 2D platformer runner, currently I made some Tilemaps that are platforming sections and then prefab, then i am trying to generate the platforming, but when I do it with a script for managing the generation of level, Instead of making the whole section it compress it in a single cell of my current grid.
public class LevelGenerator : MonoBehaviour
{
private const float PLAYER_GENERATION_DISTANCE = 200f;
[SerializeField] private Transform StartLevel;
[SerializeField] private List<Transform> levelPartsList;
[SerializeField] private GameObject player;
private Vector3 lastEndPosition;
private void Awake()
{
player = GameObject.FindWithTag("Player");
lastEndPosition = StartLevel.Find("endPosition").position;
}
void Update(){
if (Vector3.Distance(player.transform.position, lastEndPosition) < PLAYER_GENERATION_DISTANCE)
{
print("I am WOrking!!!");
SpawnLevelPart();
}
}
private void SpawnLevelPart()
{
Transform selectRandomType = levelPartsList[Random.Range(0,levelPartsList.Count)];
Transform lastLevelPartTransform = SpawnLevelPart(selectRandomType,lastEndPosition);
lastEndPosition = lastLevelPartTransform.Find("endPosition").position;
}
private Transform SpawnLevelPart(Transform levelPart,Vector3 spawnPosition)
{
Transform levelPartTransform = Tilemap.Instantiate(levelPart,spawnPosition,Quaternion.identity);
return levelPartTransform;
}
}
The compressed Tile map The prefab
When I manually put the Tilemap in the grid it works fine but how do I instantiate and then move it to the grid component as a child?
How to instantiate tilemap prefab right? - You just need to instantiate it as a child of object with "grid" component:
Second param of instantiate is a transform, that will be a parent of new instantiated object.