3-1) Creating Random SpawningPlatforms

시그니천·2024년 6월 5일

[ G01 ] ZigZagRacer

목록 보기
9/26

1. PlatformSpawner.cs 작성

public class PlatformSpawner : MonoBehaviour
{
    public GameObject platform;

    public Transform lastPlatform;
    private Vector3 lastPosition;
    private Vector3 newPos;
    private float platformSize = 2f;

    private void Start()
    {
        lastPosition = lastPlatform.position;
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            SpawnPlatforms();
        }
    }
    private void SpawnPlatforms()
    {
        GeneratePoition();

        Instantiate(platform, newPos, Quaternion.identity);

        lastPosition = newPos;
    }

    private void GeneratePoition()
    {
        newPos = lastPosition;

        // Random으로 생성
        int rand = Random.Range(0, 2);

        if(rand > 0) 
        {
            newPos.x += platformSize;
        }
        else
        {
            newPos.z += platformSize;
        }
    }
}

2. 연결

profile
우주최강개발자

0개의 댓글