유니티

[Unity 3D] 오브젝트 랜덤위치로 소환하기 - 펭귄 공먹기(2)

류수 Ryusu 2019. 9. 26. 18:38

https://suhyeon-r.tistory.com/19

 

[Unity 3D] Nav Mesh Agent 사용하기 - 펭귄 공먹기(1)

안녕하세요 ㅎㅎ Nav mesh 를 사용하여 펭귄이 자동으로 공을 먹으러 다니는 간단한 게임을 구현해보려 합니다. 먼저, 펭귄의 인스펙터 창에서 Nav Mesh Agent 를 찾아 추가해 주세요 이제는 펭귄이 다닐 수 있는..

suhyeon-r.tistory.com

지난게시글에서 네비메쉬를 사용하여 펭귄이 공을 쫒아가는것까지 만들었는데요,

이번에는 공을 여러개 나오도록 설정해보겠습니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class ballspawn : MonoBehaviour
{
    public GameObject red;
 
    public List<GameObject> redarray;
 
    // Start is called before the first frame update
    void Start()
    {
        
        redarray = new List<GameObject>();
 
        for (int i = 0; i < 5; i++)
        {
            RedSpawn();
        }
 
        InvokeRepeating("RedSpawn"101);
    }
 
    // Update is called once per frame
    void Update()
    {
        
    }
 
    void RedSpawn()
    {
        float Xpos = Random.Range(-9f, 9f);
        float Zpos = Random.Range(-8f, 8f);
        if (redarray.Count <= 10)
        {
            redarray.Add((GameObject)Instantiate(red, new Vector3(Xpos, 0.5f, Zpos), Quaternion.identity));
            
        }
    }   
}
 
 

 

저는 생성된 공을 리스트에 넣어주어 관리하였습니다.

 

InvokeRepeating("함수이름", 시간, 횟수);

이 함수는 일정 시간마다 정해진 횟수만큼 함수를 불러옵니다.

 

잘 돌아가는지 실행해보겠습니다.

 

잘 되네요 ㅎㅎ