본문 바로가기
Unity/코인먹기 게임

[유니티] 랜덤한 아이템 생성하기

by Skull Crusher 2021. 4. 27.
728x90

1. Sprite 5가지 추가

 

2. Prefabs 복제 5개

 

3. Item2 open prefabs

아이템 변경

 

4. ItemGenerator 스크립트

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ItemGenerator : MonoBehaviour
{
    public float genStartTime;
    public float genDelayTime;

    //프리팹 배열로 만들기
    public GameObject[] itemPrefabs;


    // Start is called before the first frame update
    void Start()
    {
        Vector3 tempPosition = new Vector3(0f, 2f, 0f);
        //Instantiate(itemPrefab, tempPosition, Quaternion.identity);

        //for (int i = 0; i < 10; i++)
        //{
        //    float x = Random.Range(-6f, 6f);
        //    float y = Random.Range(-4.3f, 4.3f);

        //    Vector3 randPos = new Vector3(x, y, 0f);

        //    Instantiate(itemPrefab, randPos, Quaternion.identity);

        //}
        InvokeRepeating("CreateItem", genStartTime, genDelayTime);
    }

    void CreateItem()
    {
        float x = Random.Range(-6f, 6f);
        float y = Random.Range(-4.3f, 4.3f);

        Vector3 randPos = new Vector3(x, y, 0f);

        int itemIndex = Random.Range(0, itemPrefabs.Length);

        Instantiate(itemPrefabs[itemIndex], randPos, Quaternion.identity);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

5. GameManager에 size 5개 추가 (자물쇠 잠그고 전체 드레그)

 

 

 

 

 

댓글