From f561035b950eb8577abbc87e9feec8e1d9c3b98b Mon Sep 17 00:00:00 2001 From: Mingu Kim Date: Fri, 28 Feb 2025 16:30:59 +0900 Subject: [PATCH] =?UTF-8?q?=EC=BA=90=EB=A6=AD=ED=84=B0=20=EC=98=A4?= =?UTF-8?q?=EB=B8=8C=EC=A0=9D=ED=8A=B8=20=ED=8C=8C=EA=B4=B4=20=ED=9B=84=20?= =?UTF-8?q?=EB=B0=9C=EC=83=9D=ED=95=98=EB=8A=94=20MonsterBase=20Null=20?= =?UTF-8?q?=EC=97=90=EB=9F=AC=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Assets/Gameton/Scripts/Monster/Attack.cs | 29 +++++++++++++++---- .../Gameton/Scripts/Monster/MonsterBase.cs | 9 ++++-- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/Gameton-06/Assets/Gameton/Scripts/Monster/Attack.cs b/Gameton-06/Assets/Gameton/Scripts/Monster/Attack.cs index 9d807a29..8e3c98ac 100644 --- a/Gameton-06/Assets/Gameton/Scripts/Monster/Attack.cs +++ b/Gameton-06/Assets/Gameton/Scripts/Monster/Attack.cs @@ -5,6 +5,17 @@ namespace TON public class Attack : MonoBehaviour { private MonsterBase _monsterBase; + private GameObject target; // target 변수 선언 + + void Start() + { + // target 변수 초기화 (예시) + target = GameObject.Find("TON.Player"); // Player라는 이름을 가진 게임 오브젝트 찾기 + if (target == null) + { + // Debug.LogError("Player not found!"); + } + } public void SetMonsterBase(MonsterBase monsterBase) { @@ -13,19 +24,25 @@ namespace TON private void OnTriggerEnter2D(Collider2D other) { - if (other.CompareTag("Player")) + if(target != null) // target이 null이 아닌지 확인 { - _monsterBase.IsAttacking = true; - _monsterBase.IsFinishAttack = false; // 공격 시작 시 FinishAttack 초기화 + if (other.CompareTag("Player")) + { + _monsterBase.IsAttacking = true; + _monsterBase.IsFinishAttack = false; // 공격 시작 시 FinishAttack 초기화 + } } } private void OnTriggerExit2D(Collider2D other) { - if (other.CompareTag("Player")) + if (target != null) // target이 null이 아닌지 확인 { - _monsterBase.IsAttacking = false; - _monsterBase.IsFinishAttack = true; + if (other.CompareTag("Player")) + { + _monsterBase.IsAttacking = false; + _monsterBase.IsFinishAttack = true; + } } } } diff --git a/Gameton-06/Assets/Gameton/Scripts/Monster/MonsterBase.cs b/Gameton-06/Assets/Gameton/Scripts/Monster/MonsterBase.cs index 84636a43..7466c2af 100644 --- a/Gameton-06/Assets/Gameton/Scripts/Monster/MonsterBase.cs +++ b/Gameton-06/Assets/Gameton/Scripts/Monster/MonsterBase.cs @@ -197,10 +197,13 @@ namespace TON public void Chasing() { var target = GameObject.Find("TON.Player").GetComponentInChildren(); - Vector2 direction = target.transform.position - transform.position; // 타겟과의 방향 계산 - _spriteRenderer.flipX = target.transform.position.x < transform.position.x; // 타겟이 왼쪽에 있으면 스프라이트를 왼쪽으로, 오른쪽에 있으면 오른쪽으로 바라보도록 설정 + if (target != null) + { + Vector2 direction = target.transform.position - transform.position; // 타겟과의 방향 계산 + _spriteRenderer.flipX = target.transform.position.x < transform.position.x; // 타겟이 왼쪽에 있으면 스프라이트를 왼쪽으로, 오른쪽에 있으면 오른쪽으로 바라보도록 설정 - transform.Translate(direction.normalized * moveSpeed * Time.deltaTime); // 타겟 방향으로 이동 + transform.Translate(direction.normalized * moveSpeed * Time.deltaTime); // 타겟 방향으로 이동 + } } public void MonsterSkillLaunch()