커뮤니티
포인트
쿠폰
내 강의실
국비 신청 내역
증명서
계정
로그아웃
학습 질문
개발 일지
나의 활동
답변 완료
충돌로직을 만들어서 부딛혔을때 코드가 실행되게 했는데 아무일도 일어나지 않습니다 ㅠ
undefined주차
북마크
이*목
댓글
3
추천
0
조회수
6
조회수
6
답변 완료

* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.

* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.


isTrigger와 Collision의 차이를 알게되어서 충돌했을때에 플레이어의 체력을 줄게하는 로직을 만들었는데 충돌하여도 아무런 디버그로그도 코드 작동도 안됩니다 ㅠㅠ



스파르타 즉문즉답스파르타 즉문즉답




작성한 코드 및 에러 메세지

충돌관련 코드는 젤 하단에 표시를 해두었습니다!


using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class player : MonoBehaviour

{


  public LayerMask playerLayer;


  Rigidbody2D rigid;


  public Animator animator; //공격 모션위해 사용

  

  //공격

  public Transform atkpoint;

  public float attackRange = 0.5f;

  public int attackDamage = 1;

  public LayerMask enemyLayers;



  //점프

  public float jumpSpeed = 50f;



  //패링

  public float bounceForce = 10f;

  public float cooldownTime = 1f;

  private bool canBounce = true;

  public float ParryRange = 1f;


  StatusManager theStatus;


  public GameObject[] hearts;


  public int maxHealth = 3;

  private int currentHealth;


  private void Start()

  {

    currentHealth = maxHealth;

    rigid = GetComponent<Rigidbody2D>();

    theStatus = FindObjectOfType<StatusManager>();

  }


  public void Update()

  {

    if (currentHealth < 1)

    {

      Destroy(hearts[0].gameObject);

      Die();

    }

    if (currentHealth < 2)

    {

      Destroy(hearts[1].gameObject);

    }

    if (currentHealth < 3)

    {

      Destroy(hearts[2].gameObject);

    }

  }


  //점프

  public void JumpBtn()

  {

    if (rigid.velocity.y == 0)

    {

      rigid.velocity = Vector2.up * jumpSpeed;

    }

  }



  //공격

  public void AtkBtn()

  {

    Debug.Log("나 휘둘렀쟝");

    Attack();

    StartCoroutine(AtkCoolTime());

  }


  void Attack()

  {

    animator.SetTrigger("Atk");


    Collider2D[] hitEnimies = Physics2D.OverlapCircleAll(atkpoint.position, attackRange, enemyLayers);


    foreach (Collider2D building in hitEnimies)

    {

      Debug.Log("맞았어..");

      building.GetComponent<building>().TakeDamge(attackDamage);

    }

  }


  private void OnDrawGizmosSelected()

  {

    if (atkpoint == null)

      return;


    Gizmos.DrawSphere(atkpoint.position, attackRange); //공격범위 보여줌

  }




  //패링

  public void ParryBtn()

  {

    if (canBounce)

    {

      Parry();

      StartCoroutine(ParryCoolTime());

    }

  }


  void Parry()

  {

    Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, ParryRange, playerLayer); //플레이어 레이어와 같은 물체만 튕겨냄


    foreach (Collider2D collider in colliders)

    {

      if (collider.CompareTag("building"))

      {

        Rigidbody2D rb = collider.GetComponent<Rigidbody2D>();

        if (rb != null)

        {

          // 물체를 튕겨내는 코드

          Vector2 throwDirection = (rb.position - (Vector2)transform.position).normalized;

          rb.velocity = throwDirection * bounceForce;

        }

      }

    }

  }


  IEnumerator ParryCoolTime()

  {

    canBounce = false;

    yield return new WaitForSeconds(cooldownTime);

    canBounce = true;

  }


  IEnumerator AtkCoolTime()

  {

    canBounce = false;

    yield return new WaitForSeconds(cooldownTime);

    canBounce = true;

  }




------------------------------------------------------------------------------------------------------- 여기 밑부분이 충돌 로직을 만든것 입니다

  private void OnCollisionEnter(Collision collision)

  {

    Debug.Log("부딛힘");

    if (collision.gameObject.CompareTag("building"))

    {

      TakeDamage(1);

      Debug.Log("피해받음");

    }

  }


  private void TakeDamage(int damageAmount)

  {

    currentHealth -= damageAmount;

  }


  private void Die()

  {

    Debug.Log("플레이어가 사망했습니다.");

  }

}



취소
 공유
취소
댓글 0
댓글 알림
나의얼굴