๐ŸŽฎ Unity Study/Unity

[Unity Associate Programmer ์ž๊ฒฉ์ฆ ์ค€๋น„] 2๊ฐ•. ๋™๋ฌผ ๋จน์ด์ฃผ๊ธฐ ๊ฒŒ์ž„

ibelieveinme 2023. 5. 20. 19:50
728x90

random ์œ„์น˜์—์„œ randomํ•œ ๋™๋ฌผ๋“ค์ด ์ƒ์„ฑ๋˜์–ด ๋†๋ถ€์—๊ฒŒ ๋›ฐ์–ด๋“ ๋‹ค. ๋†๋ถ€๋Š” ์ขŒ์šฐ๋กœ ์ด๋™ํ•˜๋ฉฐ ๋™๋ฌผ๋“ค์—๊ฒŒ ๋จน์ด๋ฅผ ์ค˜์•ผ ํ•œ๋‹ค.


1. Player x ๊ฐ’ ์ œํ•œํ•˜๊ธฐ

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    private const float SPEED = 10f;
    private const float X_RANGE = 13f;

    private float horizontalInput;

    void Update(){
        //keep the player in the bounds
        if (transform.position.x > X_RANGE) transform.position = new Vector3(X_RANGE, transform.position.y, transform.position.z);
        else if (transform.position.x < -X_RANGE) transform.position = new Vector3(-X_RANGE, transform.position.y, transform.position.z);

        horizontalInput = Input.GetAxis("Horizontal");
        transform.Translate(Vector3.right * Time.deltaTime * SPEED * horizontalInput);
    }
}

Update() ๋ฌธ์—์„œ Player์˜ x๊ฐ’์„ ํ™•์ธํ•ด์„œ Player๊ฐ€ ํ™”๋ฉด ๋ฐ–์œผ๋กœ ๋ฒ—์–ด๋‚˜์ง€ ๋ชปํ•˜๋„๋ก x๊ฐ’์„ ์ œํ•œํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฐธ๊ณ ) ์ƒ์ˆ˜๊ฐ’์„ ๊ทธ๋Œ€๋กœ ๋„ฃ์ง€ ๋ง๊ณ  ๊ผญ ๋ณ€์ˆ˜๋กœ ๋นผ์„œ ์ง๊ด€์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜์ž. ์˜ค๋ฅ˜๊ฐ€ ์—†๋„๋ก!

         ์ฃผ์„์„ ์Šต๊ด€ํ™”ํ•˜์ž.

 

2. ์Œ์‹ Prefab ๋งŒ๋“ค์–ด์„œ Player ์œ„์น˜์—์„œ ๋ฐœ์‚ฌํ•˜๊ธฐ.

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    private const float SPEED = 10f;
    private const float X_RANGE = 13f;
    private float horizontalInput;

    [SerializeField] GameObject projectilePrefab;


    void Update(){
        //keep the player in the bounds
        if (transform.position.x > X_RANGE) transform.position = new Vector3(X_RANGE, transform.position.y, transform.position.z);
        else if (transform.position.x < -X_RANGE) transform.position = new Vector3(-X_RANGE, transform.position.y, transform.position.z);

        horizontalInput = Input.GetAxis("Horizontal");
        transform.Translate(Vector3.right * Time.deltaTime * SPEED * horizontalInput);

        //Lauch a projectile from the player
        if (Input.GetKeyDown(KeyCode.Space)) Feed();
    }

    void Feed() {
        Instantiate(projectilePrefab, transform.position, Quaternion.identity);
    }
}

projectilePrefab์„ ์„ ์–ธํ•˜๊ณ  inspector์—์„œ prefab์„ ์—ฐ๊ฒฐํ•ด์ค€ ํ›„, Instrantiate ํ•จ์ˆ˜๋กœ Prefab์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

Instantiate ํ•จ์ˆ˜์˜ ํ˜•ํƒœ๋Š” ์œ„์™€ ๊ฐ™์€๋ฐ, ์ธ์ž๋Š” Object, position, rotation ๊ฐ’์„ ๋„ฃ์–ด์ค˜์•ผ ํ•œ๋‹ค. parent์˜ ์ž์‹์œผ๋กœ๋„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Œ.

 

3. Prefab ๋ณ€๊ฒฝ์‚ฌํ•ญ ๋ชจ๋“  Prefab์— ์ ์šฉํ•˜๊ธฐ

Prefab Inspector ์ƒ๋‹จ์— Overrides ํด๋ฆญ > Applay All ํด๋ฆญ.

 

3. ๋™๋ฌผ์ƒ์„ฑ ์œ„์น˜์— ๋žœ๋ค๊ฐ’ ์ ์šฉํ•˜๊ธฐ

using UnityEngine;

public class SpawnManager : MonoBehaviour
{
    [SerializeField] private GameObject[] animalPrefabs;

    private void Update() {
        if (Input.GetKeyDown(KeyCode.S)) {
            int animalIndex = Random.Range(0, animalPrefabs.Length);
            int randomX = Random.Range(-13, 13);

            Instantiate(animalPrefabs[animalIndex], new Vector3(randomX, 0, 20), animalPrefabs[animalIndex].transform.rotation);
        } 
    }
}
//์ •์˜
public static float Range(float minInclusive, float maxInclusive);

 

SpawnManager ํด๋ž˜์Šค ๋ฐ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์„œ ๋ณต์ œ์ƒ์„ฑํ•  ๋™๋ฌผ ๋ฐฐ์—ด์„ ๊ฐ–๊ณ  ์žˆ๊ณ  Random.Range() ํ•จ์ˆ˜๋กœ randomํ•œ ๋™๋ฌผ์„ random ์œ„์น˜์— ์ƒ์„ฑํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค. minInclusive๋Š” ์ตœ์†Œ๊ฐ’, maxInclusive๋Š” ์ตœ๋Œ€๊ฐ’์„ ์˜๋ฏธํ•œ๋‹ค.

 

float์˜ ๊ฒฝ์šฐ ์ตœ์†Œ, ์ตœ๋Œ€๊ฐ’์ด random ์ˆ˜์— ํฌํ•จ๋˜์ง€๋งŒ, int ๊ฐ’์˜ ๊ฒฝ์šฐ ์ตœ์†Œ๊ฐ’์€ ํฌํ•จ๋˜๊ณ  ์ตœ๋Œ€๊ฐ’์€ ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค.

//์ŠคํŽ˜์ด์Šค๋ฐ” ๋ˆ„๋ฅธ ์ˆœ๊ฐ„
Input.GetKeyDown(KeyCode.Space)

//์ŠคํŽ˜์ด์Šค๋ฐ” ๋ˆ„๋ฅด๋Š” ์ค‘
Input.GetKey(KeyCode.Space)

//์ŠคํŽ˜์ด์Šค๋ฐ”์—์„œ ์†๋–ผ๋Š” ์ˆœ๊ฐ„
Input.GetKeyUp(KeyCode.Space)

Input์˜ GetKeyDown, GetKey, GetKeyUp ํ•จ์ˆ˜ ์˜๋ฏธ๋Š” ์œ„์™€ ๊ฐ™๋‹ค.

 

4. Isometric View vs Top View

Isometric ๋ทฐ๋Š” ์ˆ˜์ง ์œ„์—์„œ ๋ฐ”๋ผ๋ณด๋Š” ํ™”๋ฉด์œผ๋กœ 2D ํ‘œํ˜„์„ ํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

์™ผ์ชฝ Top VIew, ์˜ค๋ฅธ์ชฝ Isometric ๋ทฐ

Scene ๋ทฐ ์„ค์ •์—์„œ Isometric์คฌ๋˜๊ฑธ Game๋ทฐ์—์„œ๋„ ์ ์šฉํ•˜๊ณ  ์‹ถ์œผ๋ฉด Camera ์—์„œ projection ๊ฐ’์„ Orthographic ์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค.

 

5. ์ผ์ • ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ์œผ๋กœ ๋™๋ฌผ ์ƒ์„ฑํ•˜๊ธฐ

using UnityEngine;

public class SpawnManager : MonoBehaviour
{
    [SerializeField] private GameObject[] animalPrefabs;

    private const float START_DELAY = 2f;
    private const float SPAWN_INTERVAL = 1.5f;

    private void Start() {
        InvokeRepeating("SpawnAnimal", START_DELAY, SPAWN_INTERVAL);
    }
    private void SpawnAnimal() {
        int animalIndex = Random.Range(0, animalPrefabs.Length);
        int randomX = Random.Range(-13, 13);

        Instantiate(animalPrefabs[animalIndex], new Vector3(randomX, 0, 20), animalPrefabs[animalIndex].transform.rotation);   
    }
}

public void InvokeRepeating(string methodName, float time, float repeatRate);

 

Update()๋ฌธ์—์„œ s ํ‚ค๋ฅผ ๋ˆŒ๋Ÿฌ์„œ ์ƒ์„ฑํ–ˆ๋˜ ๋™๋ฌผ์„ ์ผ์ • ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ์œผ๋กœ ์ƒ์„ฑ๋˜๋„๋ก ๋ณ€๊ฒฝํ–ˆ๋‹ค.

InvokeRepeating(ํ˜ธ์ถœํ•  ๋ฉ”์†Œ๋“œ ์ด๋ฆ„, ์‹œ์ž‘ ์ง€์—ฐ์‹œ๊ฐ„, ๋ฐ˜๋ณต ๊ฐ„๊ฒฉ) ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

6. ๋™๋ฌผ๊ณผ ๋จน์ด์— ์ถฉ๋Œํšจ๊ณผ ์ฃผ๊ธฐ

๋™๋ฌผ์—๋Š” Box Collider ๋งŒ ์ถ”๊ฐ€ํ•œ๋‹ค.

Collider ํฌ๊ธฐ๋Š” Size๋กœ ์กฐ์ •ํ•  ์ˆ˜๋„ ์žˆ๊ณ  Edit Collider ๋ฒ„ํŠผ์„ ํ†ตํ•ด  GUI๋กœ ์‰ฝ๊ฒŒ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Œ.

๋จน์ด์—๋Š” ์ถฉ๋Œ ๊ฐ์ง€๋ฅผ ์œ„ํ•œ Box Collider ์™€ ์ค‘๋ ฅ ํ•ด์ œ๋ฅผ ์œ„ํ•œ Rigidbody๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

์ด๋•Œ ๋จน์ด๊ฐ€ ์•„๋ž˜๋กœ ๋–จ์–ด์ง€์ง€ ์•Š๊ณ  ์•ž์œผ๋กœ ์ญ‰ ๋‚˜์•„๊ฐ€๊ธธ ์›ํ•˜๋ฏ€๋กœ Rigidbody์— Use Gravity ์†์„ฑ์€ ํ•ด์ œํ•œ๋‹ค.

using UnityEngine;

public class DetectCollisions : MonoBehaviour {

    private void OnTriggerEnter(Collider other) {
        Destroy(gameObject);
        Destroy(other.gameObject);
    }
}

๋™๋ฌผ๊ณผ ๋จน์ด ๊ฐ์ฒด์— DetectCollisions ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

OntriggerEnter(Collider other) ํ•จ์ˆ˜๋กœ ์ถฉ๋Œ์„ ์ธ์ง€ํ•œ๋‹ค. ์ถฉ๋Œ ์‹œ, DetectCollisions ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๊ฐ–๊ณ  ์žˆ๋Š” ๊ฐ์ฒด(gameObject)์™€ ์ถฉ๋Œํ•œ ๊ฐ์ฒด(other) ๋ชจ๋‘ ์ œ๊ฑฐ๋œ๋‹ค.

728x90