상세 컨텐츠

본문 제목

유니티 무기 점차 사라지게 하기(dissolve)

카테고리 없음

by MJ_119 2024. 10. 29. 23:10

본문

디졸브 효과.

 

URP -> Lit Shader Graph 만들어서 아래와 같이 설정. 

 

Position 노드에 Space에 따라 World = 월드기준, 맨위에서부터 아래로 사라지게 할것인가( 글로벌 X, Y, Z축 기준 ),

Space / object = 아니면 오브젝트의 로컬 기준으로 X, Y, Z축 기준으로 사라지게 할 것인가를 정함

 

- Split 노드의 R G B가 각각 오브젝트나 월드좌표의 X, Y, Z축을 담당하기때문에 연결을 어떻게 하냐에 따라 사라지는 부위가 달라짐.

 

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

public class WeaponEquip : MonoBehaviour
{
    public Material material;
    public float fadeOutDuration = 0.6f; // 천천히 나타나거나 사라지는 시간
    public float fadeInDuration = 0.4f; // 천천히 나타나거나 사라지는 시간

    // Coroutine으로 점차적으로 칼날을 나타나게 하는 함수
    public void WeaponEquipShader()
    {
        StartCoroutine(FadeWeaponEquipCoroutine(0.5f, 2f)); // 점차 증가
    }

    // Coroutine으로 점차적으로 칼날을 사라지게 하는 함수
    public void WeaponUnEquipShader()
    {
        StartCoroutine(FadeWeaponUnEquipCoroutine(2f, 0f)); // 점차 감소
    }

    private IEnumerator FadeWeaponEquipCoroutine(float startValue, float endValue)
    {
        float elapsedTime = 0f;

        while (elapsedTime < fadeOutDuration)
        {
            float currentStepValue = Mathf.Lerp(startValue, endValue, elapsedTime / fadeOutDuration);
            material.SetFloat("_StepValue", currentStepValue); // 현재 값을 셰이더에 전달

            elapsedTime += Time.deltaTime; // 시간을 업데이트
            yield return null; // 다음 프레임까지 대기
        }

        material.SetFloat("_StepValue", endValue); // 최종 값 설정
    }

    private IEnumerator FadeWeaponUnEquipCoroutine(float startValue, float endValue)
    {
        float elapsedTime = 0f;

        while (elapsedTime < fadeInDuration)
        {
            float currentStepValue = Mathf.Lerp(startValue, endValue, elapsedTime / fadeInDuration);
            material.SetFloat("_StepValue", currentStepValue); // 현재 값을 셰이더에 전달

            elapsedTime += Time.deltaTime; // 시간을 업데이트
            yield return null; // 다음 프레임까지 대기
        }

        material.SetFloat("_StepValue", endValue); // 최종 값 설정
    }
}