[VR] GroundZero 개발

[4Idle - GroundZero] Player 사망 연출 게임씬에 적용

meltingmelvin 2024. 1. 9. 18:22

 

-빈 오브젝트를 생성해 PlayerDie 스크립트를 넣어준다. 

-이 오브젝트를 GameMain에 할당한다.

gameMain 수정

-gameMain의 update에서 플레이어의 hp가 0이하가 될때 PlayerDie 스크립트의 PlayerDeath 메서드를 호출한다.

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayerDie : MonoBehaviour
{
    [SerializeField] GameObject teleportGo;
    [SerializeField] Canvas teleportCanvas;
    [SerializeField] Canvas dieCanvas;

    public void PlayerDeath()
    {
        Debug.Log("Player Die!");
        StartCoroutine(this.CoChangeColor());

    }

    private IEnumerator CoChangeColor()
    {
        this.teleportGo.SetActive(true);
        var images = this.teleportCanvas.GetComponentsInChildren<Image>();
    
        int countTime = 0;
        while (countTime < 20)
        {
            if (countTime % 2 == 0)
            {
                for (int i = 0; i < images.Length; i++)
                {
                    images[i].color = new Color32(77, 26, 26, 255);//to red
                }
                this.dieCanvas.gameObject.SetActive(true);
            }
            else
            {
                for (int i = 0; i < images.Length; i++)
                {
                    images[i].color = new Color32(67, 67, 67, 255);//to black
                    this.dieCanvas.gameObject.SetActive(false);
                }
            }
            yield return new WaitForSeconds(0.25f);
            countTime++;

        }
        yield return new WaitForSeconds(0.1f);
        Debug.Log("fade Out");
        this.dieCanvas.gameObject.SetActive(true);
        var image = this.dieCanvas.GetComponentInChildren<Image>();
        image.color = new Color32(0, 0, 0, 0);
        var color = image.color;
        while (true)
        {//fade out
            Debug.Log(color.a);
            color.a += 0.01f;
            image.color = color;
            if (color.a >= 1)
            {
                SceneManager.LoadScene("GameOverScene");
                break;

            }
            yield return null;
        }

    }
}