5-5. 7조 SceneChanger(Fade in, Fade out), Obstacle - Arrow

keubung·2024년 11월 19일

1. SceneChanger(Fade in, Fade out)

씬이 전환될 때 효과를 주어 부드럽게 넘어가도록 효과 추가

  • SceneChanger 오브젝트의 자식으로 FadePanel 추가
  • SceneChanger 오브젝트의 SceneChanger 컴포넌트 fadeImage에 FadePanel 추가
        //public SceneAsset scene;
        public Image fadeImage;
        public float fadeDuration = 1.5f;
    
        private static SceneChanger instance;
    
        private void Awake()
        {
            if (instance == null)
            {
                instance = this;
                DontDestroyOnLoad(gameObject);
            }
            else
            {
                Destroy(gameObject);
            }
        }
    
        public void StartButton()
        {
            //if (scene != null)
            //{
            //SceneManager.LoadScene("stage1");
            LoadSceneWithFade("stage1");
            //}
        }
    
        public void LoadGame()
        {
            string savedStage = PlayerPrefs.GetString("SavedStage", "Stage1");
            //SceneManager.LoadScene(savedStage);
            LoadSceneWithFade(savedStage);
        }
    
        public void GoToTitle()
        {
            SaveGame();
            //SceneManager.LoadScene("TitleScene");
            LoadSceneWithFade("TitleScene");
        }
    
        private void SaveGame()
        {
            string currentScene = SceneManager.GetActiveScene().name;
            PlayerPrefs.SetString("SavedStage", currentScene);
            PlayerPrefs.Save();
        }
    
        public void NextStage()
        {
            string currentScene = SceneManager.GetActiveScene().name;
            string nextScene = "";
    
            if (currentScene == "Stage1") nextScene = "Stage2";
            else if (currentScene == "Stage2") nextScene = "Stage3";
            else if (currentScene == "Stage3") nextScene = "TitleScene";
    
            //SceneManager.LoadScene(nextScene);
            LoadSceneWithFade(nextScene);
        }
    
        public void LoadSceneWithFade(string scene)
        {
            StartCoroutine(FadeOutAndLoadScene(scene));
        }
    
        private IEnumerator FadeOutAndLoadScene(string sceneName)
        {
            float elapsedTime = 0f;
            Color color = fadeImage.color;
    
            // 페이드 아웃 효과
            while (elapsedTime < fadeDuration)
            {
                elapsedTime += Time.deltaTime;
                color.a = Mathf.Lerp(0, 1, elapsedTime / fadeDuration);
                fadeImage.color = color;
                yield return null;
            }
    
            SceneManager.LoadScene(sceneName);
    
            // 페이드 인 효과 (Optional)
            elapsedTime = 0f;
            while (elapsedTime < fadeDuration)
            {
                elapsedTime += Time.deltaTime;
                color.a = Mathf.Lerp(1, 0, elapsedTime / fadeDuration);
                fadeImage.color = color;
                yield return null;
            }
        }
    
        public void Exit()
        {
    #if UNITY_EDITOR
            UnityEditor.EditorApplication.isPlaying = false;
    #else
                Application.Quit();
    #endif
        }

2. Obstacle - Arrow

  • 플레이어가 일정거리 안에 감지가 되면 화살이 일정거리를 arrowSpeed의 속도로 이동
  • 플레이어에게 닿았을 경우: 플레이어에게 데미지를 주고 오브젝트 파괴
  • 플레이어에게 닿지 않았을 경우: 일정거리를 이동한 후 오브젝트 파괴
    public float playerRange = 5f;
    public float arrowSpeed = 5f;
    public float maxDistance = 15f;
    public float damage = 20f;
    
    private GameObject player;
    private bool isFlying = false;
    private Vector3 startPosition;
    
    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player");
        startPosition = transform.position;
    }
    
    void Update()
    {
        DetectAndShoot();
        HandleArrowMovement();
    }
    
    private void DetectAndShoot()
    {
        if (!isFlying && player != null)
        {
            float distanceToPlayer = Mathf.Abs(player.transform.position.x - transform.position.x);
            if (distanceToPlayer <= playerRange)
            {
                isFlying = true;
            }
        }
    }
    
    private void HandleArrowMovement()
    {
        if (isFlying)
        {
            transform.Translate(Vector3.right * arrowSpeed * Time.deltaTime);
    
            if (Vector3.Distance(startPosition, transform.position) >= maxDistance)
            {
                Destroy(gameObject);
            }
        }
    }
    
    private void OnTriggerEnter2D(Collider2D collision)
    {
        // 플레이어와 충돌 처리
        if (collision.CompareTag("Player"))
        {
            // 플레이어에게 데미지를 준다
            PlayerInfo playerInfo = player.GetComponent<PlayerInfo>();
            if (playerInfo != null)
            {
                playerInfo.health -= damage;
            }
    
            // 화살 파괴
            Destroy(gameObject);
        }
    }
profile
김나영(Unity_6기)

0개의 댓글