๐Ÿ”ŠUnity ์†Œ์Œ์„ ์ธก์ •ํ•˜๋Š” ๋งต์„ ๋งŒ๋“ค์–ด๋ณด์ž (feat. ์„ ์ƒ๋‹˜๋ชฐ๋ž˜์ถค์ถ”๊ธฐ๊ฒŒ์ž„)

Se0ng_1lยท2022๋…„ 9์›” 28์ผ
0

์ผ์ƒ Unity

๋ชฉ๋ก ๋ณด๊ธฐ
4/9

์†Œ์Œ์„ ์ธก์ •ํ•˜๋Š” ๋งต์„ ๋งŒ๋“ค์–ด๋ณด์ž

์œ ๋ช…ํ•œ ํ”Œ๋ž˜์‹œ๊ฒŒ์ž„ ์„ ์ƒ๋‹˜ ์•ˆ๋ณผ๋•Œ ์ถค์ถ”๊ธฐ ๊ฒŒ์ž„์„ ์‘์šฉ

์ด ๊ฒŒ์ž„์— ๋Œ€ํ•œ ์†Œ๊ฐœ :

์„ ์ƒ๋‹˜์ด ์ˆ˜์—…ํ•˜๋Š” ๋™์•ˆ ๋งˆ์šฐ์Šค ํด๋ฆญ์„ ํ†ตํ•ด ๋“คํ‚ค์ง€ ์•Š๊ณ  ์ถค์„ ์ถ”๋ฉด์„œ ๋ฒ„ํ…จ ์‹œ๊ฐ„์— ๋”ฐ๋ผ ์ ์ˆ˜๋ฅผ ์–ป๋Š” ๊ฒŒ์ž„์ด๋‹ค.

๐ŸŽฅ๋ฏธ๋ฆฌ๋ณด๊ธฐ

โญ๏ธ์„ค๋ช… ์‹œ์ž‘

1. ์ง€ํ˜•์„ ๋งŒ๋“ค๊ณ  ํŠธ๋ฆฌ๊ฑฐ๋ฅผ ์ž‘๋™์‹œํ‚ฌ Empty Object๋ฅผ ์ƒ์„ฑํ•œ ํ›„ BoxCollider๋ฅผ ๋„ฃ๋Š”๋‹ค. (๋นจ๊ฐ„์ƒ‰์ด ์ง€์—ญ์ด ์†Œ์Œ์„ ์ธก์ •ํ•  ์ง€์—ญ)

2. UI๋ฅผ ํ†ตํ•ด ์†Œ์Œ์˜ ์ •๋„๋ฅผ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด UI - Slider๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

3. ์บ๋ฆญํ„ฐ ์œ„์— UI๋ฅผ ํ‘œ์‹œํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— Hierarchy - Canvas - Canvas ์ปดํฌ๋„ŒํŠธ - Render Mode - World Space๋ฅผ ์„ ํƒํ•œ๋‹ค.


์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ•ด๋‹น Canvas ๋ฐ ์†ํ•œ UI์˜ค๋ธŒ์ ํŠธ๋“ค์€ ์›”๋“œ์ƒ์— ์กด์žฌํ•˜๊ฒŒ ๋œ๋‹ค.

4 - 1. ์šฐ๋ฆฌ๋Š” Slider์˜ ์†์žก์ด๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์‚ญ์ œ

4 - 2. Slider๋ฅผ ๋”ฐ๋กœ ์กฐ์ž‘ํ•˜์ง€ ์•Š์„ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜ํ–‰ํ•œ๋‹ค.

4 - 3. Slider์˜ค๋ธŒ์ ํŠธ์˜ ์ž์‹ ์˜ค๋ธŒ์ ํŠธ ์ค‘ Fill๊ณผ BackGround์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ž์‹ ์ด ์›ํ•˜๋Š” ๋ชจ์–‘๊ณผ ์ƒ‰์œผ๋กœ ์ˆ˜์ •ํ•œ๋‹ค.


Fill : ์ฑ„์šธ ์ƒ‰๊น”
BackGround : Slider์˜ ์ „์ฒด ํฌ๊ธฐ ๋ณด์—ฌ์คŒ

4 - 4. Slider์˜ค๋ธŒ์ ํŠธ์˜ ์ž์‹ ์˜ค๋ธŒ์ ํŠธ BackGround, Fill Area, Fill์„ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ชจ๋‘ 0์œผ๋กœ ์„ค์ •ํ•œ๋‹ค.


์ด ์ž‘์—…์„ ํ•˜๋Š” ์ด์œ ๋Š” ํ•˜์ง€ ์•Š์œผ๋ฉด ์Šฌ๋ผ์ด๋”๊ฐ€ ๊ฝ‰ ์ฑ„์›Œ์ง€์ง€ ์•Š์•„ ๋นˆ๊ณต๊ฐ„์ด ์ƒ๊ธฐ๋Š”๋ฐ ์ˆ˜ํ–‰ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฝ‰ ์ฑ„์šธ ์ˆ˜ ์žˆ๋‹ค.

4 - 5. ์บ๋ฆญํ„ฐ ์œ„์— UI๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๋„๋ก ์บ”๋ฒ„์Šค์™€ Slider์˜ ํฌ๊ธฐ๋ฅผ ์ ๋‹นํžˆ ์กฐ์ ˆํ•˜์—ฌ ๋ฐฐ์น˜ํ•œ๋‹ค.

5. ํŠธ๋ฆฌ๊ฑฐ๋ฅผ ์ž‘๋™์‹œํ‚ฌ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•ด๋ณด์ž

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; // UI๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•จ

public class NoiseCheck : MonoBehaviour
{
    [SerializeField] private GameObject player; 
    // ์บ๋ฆญํ„ฐ ์˜ค๋ธŒ์ ํŠธ
    [SerializeField] private GameObject UIObject; 
    // Canvas์˜ค๋ธŒ์ ํŠธ
    [SerializeField] private Slider noiseSlider;
    // ์Šฌ๋ผ์ด๋” ์˜ค๋ธŒ์ ํŠธ
    [SerializeField] private Image fillImage;
    // ์Šฌ๋ผ์ด๋”์˜ ๊ฐ’์— ๋”ฐ๋ผ ์Šฌ๋ผ์ด๋”์˜ ์ƒ‰์— ๋ณ€ํ™”๋ฅผ ์ฃผ๊ธฐ์œ„ํ•œ ์ด๋ฏธ์ง€

    // ์บ๋ฆญํ„ฐ๊ฐ€ ๋“ค์–ด์˜ฌ๋•Œ๋งŒ ์ˆ˜ํ–‰์‹œํ‚ค๊ธฐ
    private bool isPlayerEnter;
    
    private float chargeSpeed; 
    // ์ฑ„์›Œ์ง€๋Š” ์†๋„
    private float chargeTime; 
    // ์ฑ„์›Œ์ง€๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„
    private float currentNoise; 
    // ํ˜„์žฌ ์Šฌ๋ผ์ด๋” ๊ฐ’์„ ์ €์žฅ

    public float walkingTime = 15f;
    // ์บ๋ฆญํ„ฐ๊ฐ€ ๊ฑธ์„ ๋•Œ
    public float runningTime = 3f;
    // ์บ๋ฆญํ„ฐ๊ฐ€ ๋‹ฌ๋ฆด ๋•Œ์˜ chargeTime
    public float waitingTime = 4f;
    // ์บ๋ฆญํ„ฐ๊ฐ€ ๋Œ€๊ธฐํ•˜๊ณ  ์žˆ์„ ๋•Œ
    public float sliderYOffset = 6f;
    // ์บ๋ฆญํ„ฐ์™€ ์Šฌ๋ผ์ด๋” ์‚ฌ์ด์˜ ๊ณ ์ • ์˜คํ”„์…‹
    
    void Update()
    {
        if (isPlayerEnter)
        {
            // ์บ๋ฆญํ„ฐ๊ฐ€ ์›€์ง์ด๋ฉด UI๋„ ๋™์‹œ์— ์›€์ง์ผ ์ˆ˜ ์žˆ๋„๋ก ์ˆ˜ํ–‰
            UIObject.transform.position = new Vector3(player.transform.position.x,
                player.transform.position.y + sliderYOffset,
                player.transform.position.z);

            // ํŠธ๋ฆฌ๊ฑฐ ๋‚ด์—์„œ ์บ๋ฆญํ„ฐ ์›€์ง์ž„ ๊ด€๋ จ ์ž…๋ ฅ์„ ๋ฐ›์œผ๋ฉด ์ˆ˜ํ–‰    
            if (Input.GetKey(KeySetting.keys[KeyAction.LEFT]) || Input.GetKey(KeySetting.keys[KeyAction.RIGHT]) ||
                Input.GetKey(KeySetting.keys[KeyAction.UP]) || Input.GetKey(KeySetting.keys[KeyAction.Down]))
            {
                if (Input.GetKey(KeySetting.keys[KeyAction.WALK]))
                {
                    chargeTime = walkingTime;
                }
                else
                {
                    chargeTime = runningTime;
                }
            }
            else
            {
                chargeTime = -waitingTime;
            }

            // ์†๋„ = ๊ฑฐ๋ฆฌ / ์‹œ๊ฐ„ => 
            // ์ฑ„์›Œ์ง€๋Š” ์†๋„ = (์Šฌ๋ผ์ด๋” ์ตœ๋Œ€๊ฐ’ - ์ตœ์†Œ๊ฐ’) / ์ƒํ™ฉ์— ๋”ฐ๋ผ ์„ค์ •ํ•œ ์‹œ๊ฐ„๊ฐ’
            chargeSpeed = (noiseSlider.maxValue - noiseSlider.minValue) / chargeTime;

            // ์‹ค์ œ ์‹œ๊ฐ„๊ณผ ๋˜‘๊ฐ™์ด ๊ฑธ๋ฆด ์ˆ˜ ์žˆ๋„๋ก Time.deltaTime์„ ๊ณฑํ•˜๊ธฐ
            currentNoise += chargeSpeed * Time.deltaTime;

            // ํ˜„์žฌ ๊ฐ’์ด ์Šฌ๋ผ์ด๋” ์ตœ๋Œ€๊ฐ’ ์ด์ƒ์ด๋ฉด ์ตœ๋Œ€๊ฐ’์œผ๋กœ
            if (currentNoise >= noiseSlider.maxValue)
            {
                currentNoise = noiseSlider.maxValue;
            }
            // ํ˜„์žฌ ๊ฐ’์ด ์Šฌ๋ผ์ด๋” ์ตœ์†Œ๊ฐ’ ์ดํ•˜๋ผ๋ฉด ์ตœ์†Œ๊ฐ’์œผ๋กœ
            else if (currentNoise <= noiseSlider.minValue)
            {
                currentNoise = noiseSlider.minValue;
            }

            ColorChange();
            // ์Šฌ๋ผ์ด๋”์— ์ ์šฉ
            noiseSlider.value = currentNoise;
        }
    }

	// ์Šฌ๋ผ์ด๋”์˜ ํ˜„์žฌ๊ฐ’์— ๋”ฐ๋ผ ์ ์šฉํ•  ์ƒ‰๊น” ์ง€์ •
    void ColorChange() 
    {
        if(currentNoise >= 0f && currentNoise <= 40f)
            fillImage.color = Color.green;
        else if(currentNoise > 40f && currentNoise <= 70f)
            fillImage.color = Color.yellow;
        else
            fillImage.color = Color.red;
    }

	// ์บ๋ฆญํ„ฐ๊ฐ€ ํŠธ๋ฆฌ๊ฑฐ์— ๋“ค์–ด์˜ค๋ฉด UI์˜ค๋ธŒ์ ํŠธ ์ผœ๊ธฐ
    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            UIObject.SetActive(true);
            isPlayerEnter = true;
        }
    }


	// ์บ๋ฆญํ„ฐ๊ฐ€ ํŠธ๋ฆฌ๊ฑฐ์— ๋“ค์–ด์˜ค๋ฉด UI์˜ค๋ธŒ์ ํŠธ ๋„๊ธฐ
    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {            
            // ๋‹ค์‹œ ๋“ค์–ด์˜ฌ ๊ฒฝ์šฐ๋ฅผ ๋Œ€๋น„ํ•ด ํ˜„์žฌ ์†Œ์Œ ๊ฐ’ ์ดˆ๊ธฐํ™” ์‹œ์ผœ๋†“๊ธฐ
            currentNoise = noiseSlider.minValue;
            UIObject.SetActive(false);
            isPlayerEnter = false;
        }
    }
}

6. ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋„ฃ๊ณ  ์ž์‹ ์˜ ํ™˜๊ฒฝ์— ๋งž๊ฒŒ ์„ค์ •ํ•˜๊ณ  Canvas์˜ค๋ธŒ์ ํŠธ๋Š” ๋„์ž

7. ๊ฒฐ๊ณผ

profile
์น˜ํƒ€๊ฐ€ ๋˜๊ณ  ์‹ถ์€ ์ทจ์ค€์ƒ

0๊ฐœ์˜ ๋Œ“๊ธ€