5-6. 7조 Obstacle 수정 및 적용

keubung·2024년 11월 20일

1. Obstacle - Trap.cs

  • 스크립트 수정 - PlayerStats.cs의 Hit 메서드 활용 (플레이어에 데미지 적용)

    private IEnumerator ApplyDamage(Collider2D player)
    {
        isDamaging = true;
    
        PlayerStats playerStats = player.GetComponent<PlayerStats>();
        if (playerStats == null)
        {
            yield break;
        }
    
        while (isDamaging)
        {
            playerStats.Hit(damagePerSecond);
            yield return new WaitForSeconds(1f); // 1초 간격으로 데미지 적용
        }
    }
  • 스크립트 적용

    1. TrapTileMapCollider 추가
    2. Trap.cs 추가
    3. Collider가 필요한 부분에 타일을 그리고 Tilemap Collider 2D 추가(필요에 따라 Rigidbody 2D, Composite Collider 2D 추가)
    4. Collider의 isTrigger활성화
    5. 필요에 따라 Tilemap Renderer 비활성화

2. Obstacle - Arrow.cs

  • 스크립트 수정
      private void DetectAndShoot()
    {
        if (!isFlying && player != null)
        {
            float distanceToPlayerX = Mathf.Abs(player.transform.position.x - transform.position.x);
            float distanceToPlayerY = Mathf.Abs(player.transform.position.y - transform.position.y);
    
            bool isPlayerInPositiveDirection = player.transform.position.x > transform.position.x;
            bool isPlayerOnSameHeight = distanceToPlayerY <= 0.5f; // 허용 오차 0.5
    
            if (distanceToPlayerX <= playerRange && isPlayerInPositiveDirection && isPlayerOnSameHeight)
            {
                isFlying = true;
            }
        }
    }
    
    private void OnTriggerEnter2D(Collider2D collision)
    {
        // 플레이어와 충돌 처리
        if (collision.CompareTag("Player"))
        {
            // 플레이어에게 데미지를 준다
            PlayerStats playerStats = player.GetComponent<PlayerStats>();
            if (playerStats != null)
            {
                playerStats.Hit(damage);
            }
    
            // 화살 파괴
            Destroy(gameObject);
        }
    }
  • 스크립트 적용
    1. Arrow 오브젝트에 Arrow.cs 추가
    2. Collider 추가 (필요에 따라 Rigidbody 2D 추가)
    3. Arrow 오브젝트 프리팹화

Composite Collider 2D

  • 다수의 2D 콜라이더를 하나의 콜라이더로 합쳐주는 역할을 하는 컴포넌트
  • 콜라이더와 콜라이더 사이에 틈이 없지만 따로 기능을 수행하여 끼이는것을 방지하기 위해 사용

	오늘 장애물이 작동하지 않아 고생했는데 collider2D가 아닌 collider를 사용해서 생긴 문제였음.
      -> 사소한 거.. 확인...

내일 계획

  • 세이브로드
  • 씬 전환 수정 - 페이드 아웃, 스테이지 전환
profile
김나영(Unity_6기)

0개의 댓글