. GameObject original

- 생성하고자 하는 게임오브젝트명. 현재 씬에 있는 게임오브젝트나 Prefab으로 선언된 객체를 의미함.

 

2. Vector3 position

- Vector3으로 생성될 위치를 설정함.

 

3. Quaternion rotation

- 생성될 게임오브젝트의 회전값을 지정한다. 

- 회전을 굳이 줘야할 상황이 아니라면, 그냥 기본값으로 설정하는 것. --> Quaternion.identity

- 또는 게임오브젝트에서 설정된 회전값. 즉, original.transform.rotation으로 작성해도 됨.

 

아래와 같이 선언해서 obj라는 게임오브젝트 객체를 동적으로 생성한다.

 

Instantiate(obj, new Vector3(x,y,z), Quaternion.identity);      // 그냥 회전없음.

또는 

Instantiate(obj, new Vector3(x,y,z), obj.transform.rotation);   // obj의 회전값.

 

 

 

============================================================================================

 

코루틴

 

 

 

// 응용 

 

                                                           Update()에서 호출하기

 

#SpawnManager.cs

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

namespace MyDefence
{


    public class SpawnManager : MonoBehaviour
    {
        //필드
        #region Variable
        //enemy 프리팹
        public GameObject enemyPrefab;
        //스폰 위치(적 시작 위치)
        public Transform startPoint;
        //스폰 타이머       
        public float spwanTimer = 5f;
        private float countdown = 0f;
        #endregion


        // Start is called before the first frame update
        void Start()
        {
            //시작지점 위치에 Enemy 1개를 생성
            //SpawnEnemy();
            //5초 간격으로 Enemy 1마리씩 스폰하기

        }

        // Update is called once per frame
        void Update()
        {
            ////5초 간격으로 Enemy 1마리씩 스폰하기 //12345
            //countdown += Time.deltaTime;
            //Debug.Log($"countdown: {countdown}");

            //if (countdown  >= spwanTimer)
            //{
            //    SpawnEnemy();

            //    //초기화
            //    countdown = 0;
            //}

            //5초 간격으로 Enemy 1마리씩 스폰하기 //54321
            //countdown -= Time.deltaTime;
            //Debug.Log($"countdown: {countdown}");

            //if (countdown <= 0f)
            //{
            //    SpawnEnemy();

            //    //초기화
            //    countdown = spwanTimer;
            //}


            //5초 간격으로 Enemy 1마리씩 스폰하기 //12345
            countdown += Time.deltaTime;
            Debug.Log($"countdown: {countdown}");

            if (countdown >= spwanTimer)
            {
                SpawnEnemy();

                //초기화
                countdown = 0;
            }

        }

        //시작지점 위치에 Enemy 를 생성
        private void SpawnEnemy()
        {

            Instantiate(enemyPrefab, startPoint.position, Quaternion.identity);

        }

    }
}

 

 

+ Recent posts