Unity

3D 테스트 프로젝트 15

psb08 2025. 3. 8. 15:47
728x90
반응형

지나가다가 Light를 발견하게 되어서 얘를 껐다 켰다가 가능할까? 얘의 색을 바꿀 수 있을까? 싶어서 만들었습니다.

 

LightTest.cs

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

public class LightTest : MonoBehaviour
{
    [SerializeField] private Light testLight;
    public int value;
    private bool isOn;

    private void Start()
    {
        StartCoroutine(ChangeColor());
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            OnOff();
            isOn = !isOn;
        }
    }

    private void OnOff()
    {
        if (isOn)
            testLight.intensity = 0;
        else
            testLight.intensity = value;
    }

    private IEnumerator ChangeColor()
    {
        while (true)
        {
            Color randomColor = GetRandomColorInHSV();
            testLight.color = randomColor;

            yield return new WaitForSeconds(0.5f);
        }
    }

    private Color GetRandomColorInHSV()
    {
        float h = Random.Range(0f, 1f);

        return Color.HSVToRGB(h, 1, 1);
    }

}

Q키를 누를 때 마다 불이 껐다가 켜지고 (정확히는 밝기를 0에서 20으로 조정) , 

0.5초마다 색이 랜덤으로 바뀝니다. 수치는 HSV로 해서 0에서 1로 설정했습니다.

 

 

일부러 제일 큰 Light는 끄고 실험했습니다.

Q를 누르면 켜지고 켜져 있다면 꺼집니다.

색도 계속 바뀌는 것을 볼 수 있습니다.

'Unity' 카테고리의 다른 글

수업 내용 복습 2  (0) 2025.03.10
수업 내용 복습 1  (0) 2025.03.09
3D 테스트 프로젝트 14  (0) 2025.03.07
3D 테스트 프로젝트 13  (0) 2025.03.06
3D 테스트 프로젝트 12  (0) 2025.03.05