๐Ÿโ˜˜๏ธ๐Ÿ๐Ÿฅฌ๐Ÿฅฆ๐ŸŒณ๐ŸŒฒ
์นดํ…Œ๊ณ ๋ฆฌ
์ž‘์„ฑ์ผ
2023. 8. 1. 14:09
์ž‘์„ฑ์ž
ห—ห‹ เญจเญง หŠห—
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RandomPoint : MonoBehaviour
{
    // ์‹œ๊ฐ„์„ ๋‹ด๋‹นํ•  ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์ค€๋‹ค.
    public float currTime;
    public GameObject randomSpawn;

    // ๋ฐ˜๋ณต๋˜๋Š” ์ž‘์—…์ด๋ฏ€๋กœ ์—…๋ฐ์ดํŠธ ํ•จ์ˆ˜ ์•ˆ์—์„œ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.
    public void Update()
    {
        // ์‹œ๊ฐ„์ด ํ๋ฅด๊ฒŒ ๋งŒ๋“ค์–ด์ค€๋‹ค.
        currTime += Time.deltaTime;

        // ์˜ค๋ธŒ์ ํŠธ๋ฅผ ๋ช‡์ดˆ๋งˆ๋‹ค ์ƒ์„ฑํ•  ๊ฒƒ์ธ์ง€ ์กฐ๊ฑด๋ฌธ์œผ๋กœ ๋งŒ๋“ ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” 10์ดˆ๋กœ ํ–ˆ๋‹ค.
        if (currTime > 1)
        {
            // x,y,z ์ขŒํ‘œ๊ฐ’์„ ๊ฐ๊ฐ ๋‹ค๋ฅธ ๋ฒ”์œ„์—์„œ ๋žœ๋คํ•˜๊ฒŒ ์ •ํ•ด์ง€๋„๋ก ๋งŒ๋“ค์—ˆ๋‹ค.
            float newX = Random.Range(-10f, 10f), newZ = Random.Range(-100f, 100f);

            // ์ƒ์„ฑํ•  ์˜ค๋ธŒ์ ํŠธ๋ฅผ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.
            //Instantiate(randomSpawn);
            Instantiate(randomSpawn);

            // ๋ถˆ๋Ÿฌ์˜จ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ๋žœ๋คํ•˜๊ฒŒ ์ƒ์„ฑํ•œ ์ขŒํ‘œ๊ฐ’์œผ๋กœ ์œ„์น˜๋ฅผ ์˜ฎ๊ธด๋‹ค.
            randomSpawn.transform.position = new Vector3(newX, transform.position.y, newZ);

            // ์‹œ๊ฐ„์„ 0์œผ๋กœ ๋˜๋Œ๋ ค์ฃผ๋ฉด, 10์ดˆ๋งˆ๋‹ค ๋ฐ˜๋ณต๋œ๋‹ค.
            currTime = 0;
        }
    }
}