728x90
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MapGenerator : MonoBehaviour
{
[Header("[Blocks]")]
public GameObject BlocksParents;
public GameObject B_DirtPrefab;
public GameObject B_BedRockPrefab;
public GameObject B_DiamondPrefab;
public GameObject B_GrassPrefab;
[Header("[Size Values]")]
public int Width_x = 0;
public int Width_z = 0;
[Header("[perlin noise]")]
public float Wavelength = 0;
public float Amplitude = 0;
private List<GameObject> BlockList = new List<GameObject>();
// Start is called before the first frame update
void Start()
{
for(int x =0;x<Width_x;x++){
for(int z = 0; z < Width_z ;z++){
BlockList.Add((GameObject) Instantiate(B_DirtPrefab,new Vector3(x, 0, z),Quaternion.identity, BlocksParents.transform));
}
}
for(int i= 0 ; i<BlockList.Count;i++){
float xCoord = (BlockList[i].transform.position.x) / Wavelength;
float zCoord = (BlockList[i].transform.position.z) / Wavelength;
int y = (int)(Mathf.PerlinNoise(xCoord,zCoord) * Amplitude);
BlockList[i].transform.position = new Vector3(BlockList[i].transform.position.x ,y, BlockList[i].transform.position.z);
}
}
}
프리펩을 생성하고 Instantiate함수로 블록을 생성해준다.
블록을 생성한 후에 리스트에 저장시키고 각 블록의 y값을 조정해준다.
이 때, 조정해주는 함수는 Mathf클래스의 PeerlinNoise함수를 사용해준다.
이를 이용하면 자연스러운 맵 생성이 가능하다.
변수 중 Wavelength는 파장, Amplitude는 진폭이다. 이를 적절히 사용하여 생성해주면 다음과 같이 생성된다.
728x90
'Develop > Imitation_MineCraft' 카테고리의 다른 글
[Imitation_MineCraft] 맵 생성하기2 (0) | 2022.12.01 |
---|