김김김의 게임개발
  • 유니티 게임개발 입문 #7
    2023년 09월 12일 22시 20분 19초에 업로드 된 글입니다.
    작성자: noun06

    플레이어 HP 및 운석 충돌 시스템

    • 플레이어와 운석 파편 충돌 시스템을 구현하였음. 운석 파편은 파티클 시스템이여서 기존 오브젝트간 충돌과 조금 방식이 달랐음. OnParticleCollision 메서드를 사용하였으며 추후에 파티클의 파티클 충돌효과를 추가할 예정임.
    • HP같은 경우 기존 하트들을 UI요소로서 Image 배열로 처음 제작하였음. 하지만 기존 UIManager과의 사용이 개인적으로 까다로웠음. PlayerHealth에서 GameManager에서 호출하고 다시 Stage에서 호출하는 방식으로 프리팹화된 하트 UI를 불러와야하는 전체적인 과정에서 접근 부분에서 오류가 발생함. 그래서 하트 이미지를 스프라이트렌더러 배열로 바꿔 플레이어의 자식 오브젝트로 넣어주는 것으로 타협을 했음.
    • 추가로 난이도 조절을 위해 파편에 부딫혔을 시 일정 시간동안 무적상태가 되는 기능을 구현하였음. 
    //PlayerHealth.cs
    
    public class PlayerHealth : MonoBehaviour
    {
        public int playerHealth;
    
        [SerializeField] private SpriteRenderer[] hearts;
        [SerializeField] private Color invincibleColor;
        [SerializeField] private int meteorDamage;
        [SerializeField] private float invincibleTime = 3f;
    
        private bool isInvincible = false;
    
        private SpriteRenderer playerSpriteRenderer;
    
        private void Start()
        {
            playerSpriteRenderer = GetComponent<SpriteRenderer>();
            UpdateHealth();
        }
    
        public void UpdateHealth()
        {
            if (playerHealth <= 0)
            {
                GameManager.Instance.GameOver();
            }
    
            for (int i = 0; i < hearts.Length; i++)
            {
                if (i < playerHealth)
                {
                    hearts[i].color = Color.white;
                }
                else
                {
                    hearts[i].color = Color.black;
                }
            }
        }
    
        public void PaddleInvincible()
        {
            isInvincible = true;
            StartCoroutine(PaddleNotInvincible());
    
            if (playerSpriteRenderer != null)
            {
                playerSpriteRenderer.color = invincibleColor;
            }
        }
    
        private IEnumerator PaddleNotInvincible()
        {
            yield return new WaitForSeconds(invincibleTime);
            isInvincible = false;
    
            if (playerSpriteRenderer != null)
            {
                playerSpriteRenderer.color = Color.white;
            }
        }
    
        public bool IsInvincible()
        {
            return isInvincible;
        }
    
        private void OnParticleCollision(GameObject other)
        {
            if (!IsInvincible())
            {
                TakeDamage(meteorDamage);
            }
        }
    
        public void TakeDamage(int damage)
        {
            playerHealth -= damage;
            UpdateHealth();
            PaddleInvincible();
        }
    }

     

    HP 증가 아이템

    • 팀원분이 만드신 기존 아이템 관련 스크립트에 PlayerHealth 클래스의 요소를 가져와 HP를 증가시키는 아이템을 추가함.
    //Paddle.cs
    
    IEnumerator Item_add_life(bool skip)
        {
            if (!skip)
            {
                _playerHealth.playerHealth += 1;
                _playerHealth.UpdateHealth();
                yield return new WaitForSeconds(0);
            }
        }

     

     

    댓글