ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Parrying Sword #26 : [기획][프로그래밍] 화면 가장자리에 나침반 화살표 만들기
    유니티 게임 개발/메인 프로젝트: Parrying Sowrd 2023. 5. 18. 10:59

     

     

    보스전에서는 필요 없지만, 일반 맵 탐색은 규모가 큰 미로 탐색이니 어느정도 길을 찾기 위한 힌트가 필요합니다.

    그래서, 맵 가장자리에 탐색을 종료하는 게이트의 위치를 가리키는 화살표를 만들어주기로 하였습니다.

     

    플레이어와 게이트의 위치를 기반으로 간단한 1차함수를 만들고, 기울기와 좌표를 통해 화면의 좌우, 혹은 위아래를 지날지 알아낸 뒤 

     

    자세한 좌표는 만든 함수에 가장자리의 x좌표 혹은 y좌표 (정확히는 플레이어 위치 기준 세로 +-10 지점, 가로 +-17 지점으로 설정하였음)를 넣어주기만 하면 됩니다.

     

    그 다음 해당 좌표를 기준으로 화살표의 위치를 서브 카메라의 위치로 이동시키기 위해 좌표를 보정해주었습니다.

     

    굳이 서브 카메라로 옮긴 이유는 플레이어 옆에서 떠다니고 있으면 메인 카메라의 영향을 받게 되는데,

    이 경우 줌인, 줌아웃이나 맵에 따른 카메라 사이즈 차이가 영향을 끼칠 수 있어서

    이에 영향을 받지 않는 UI를 표시하고 있는 서브 카메라에서 보여줄 필요성이 있었습니다.

     

    그리고 어느 정도 게이트와 가까워지면 굳이 화살표를 표시해줄 필요가 없으니 스프라이트 렌더러를 끌 수 있도록 하였습니다.

     

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    // 서브 카메라 하위에서 움직임, 가로 반경 17 세로 반경 10
    
    public class Compass : MonoBehaviour
    {
        private Transform normalGate;
        private Transform player;
        private SpriteRenderer sprite;
        private float inclination;
        private Vector3 normalGatePos;
    
    
        // Awake 단계에서 맵이 생성되니 Start에서 찾아야 함
        private void Start()
        {
            GameObject gateobject;
            gateobject = GameObject.Find("NormalGate");
    
            if (!gateobject)
            {
                this.gameObject.SetActive(false);
                return;
            }
    
            normalGate = gateobject.transform;
            sprite = gameObject.GetComponent<SpriteRenderer>();
            player = GameObject.Find("Player").transform; 
        }
    
    
        private void FixedUpdate()
        {
            if( Mathf.Abs( normalGate.position.x - player.position.x ) < 19.0 &&
                Mathf.Abs( normalGate.position.y - player.position.y ) < 12.0f )
            {
                sprite.enabled = false;
            }
            else
            {
                sprite.enabled = true;
            }
    
            inclination = getInclination();
    
            transform.rotation = Quaternion.Euler(new Vector3(0.0f, 0.0f,
                                Mathf.Atan2(    normalGate.position.y - player.position.y,
                                                normalGate.position.x - player.position.x)  * Mathf.Rad2Deg - 90.0f ));
            // Debug.Log(Vector2.Angle(player.position, normalGate.position));
    
            if (inclination > 10.0f / 17.0f || inclination < - (10.0f / 17.0f))  // 기울기가 커서 직선이 위아래를 지나가는 경우
            {
                if (player.position.y < normalGate.position.y)   // 위
                {
                    transform.position = new Vector3(xPosfunctionForCeiling() + transform.parent.position.x,
                                                    10.0f + transform.parent.position.y, transform.position.z);
                }
                else // 아래
                {
                    transform.position = new Vector3(xPosfunctionForBottom() + transform.parent.position.x,
                                                    -10.0f + transform.parent.position.y, transform.position.z);
                }
            }
            else                        // 기울기가 작아서 직선이 좌우를 지나가는 경우
            {
                if (player.position.x < normalGate.position.x)   // 오른쪽
                {
                    transform.position = new Vector3(17.0f + transform.parent.position.x,
                                                    yPosFunctionForRight() + transform.parent.position.y, transform.position.z);
                }
                else // 왼쪽
                {
                    transform.position = new Vector3(-17.0f + transform.parent.position.x,
                                                    yPosFunctionForLeft() + transform.parent.position.y, transform.position.z);
                }
            }
        }
    
    
    
        private float getInclination()
        {
            float denominator = normalGate.position.x - player.position.x;
            float numerator = normalGate.position.y - player.position.y;
    
            if( numerator == 0.0f )
            {
                return 0.01f;
            }
    
            if ( denominator == 0.0f)
            {
                denominator = 0.01f;
            }
    
            return numerator / denominator;
        }
    
        private float xPosfunctionForCeiling()
        {
            return (player.position.y + 10 - normalGate.position.y) /
                inclination + normalGate.position.x - player.position.x;
        }
    
        private float xPosfunctionForBottom()
        {
            return (player.position.y - 10 - normalGate.position.y) /
                inclination + normalGate.position.x - player.position.x;
        }
    
        private float yPosFunctionForRight()
        {
            return inclination * (player.position.x + 17 - normalGate.position.x) +
                normalGate.position.y - player.position.y;
        }
    
        private float yPosFunctionForLeft()
        {
            return inclination * (player.position.x - 17 - normalGate.position.x) +
                normalGate.position.y - player.position.y;
        }
    }
Designed by Tistory.