Moster.cs
using System.Collections;
using UnityEngine;
public class Monster : MonoBehaviour
{
public int currentHp = 1;
public float moveSpeed = 5f;
public float jumpPower = 10;
public float atkCoolTime = 3f;
public float atkCoolTimeCalc = 3f;
public bool isHit = false;
public bool isGround = true;
public bool canAtk = true;
public bool MonsterDirRight;
protected Rigidbody2D rb;
protected BoxCollider2D boxCollider;
public GameObject hitBoxCollider;
public Animator Anim;
//플래폼감지
public LayerMask layerMask;
protected void Awake ( )
{ //컴포넌트 로드
rb = GetComponent<Rigidbody2D> ( );
boxCollider = GetComponent<BoxCollider2D> ( );
Anim = GetComponent<Animator> ( );
StartCoroutine ( CalcCoolTime ( ) );
StartCoroutine ( ResetCollider ( ) );
}
IEnumerator ResetCollider ( )
{ //몬스터 죽었을때 꺼진 히트박스 다시 켜줌
while ( true )
{
yield return null;
if ( !hitBoxCollider.activeInHierarchy )
{
yield return new WaitForSeconds ( 0.5f );
hitBoxCollider.SetActive ( true );
isHit = false;
}
}
}
//공격가능한쿨타임
IEnumerator CalcCoolTime ( )
{
while ( true )
{
yield return null;
if ( !canAtk )
{
atkCoolTimeCalc -= Time.deltaTime;
if ( atkCoolTimeCalc <= 0 )
{
atkCoolTimeCalc = atkCoolTime;
canAtk = true;
}
}
}
}
public bool IsPlayingAnim ( string AnimName )
{
if ( Anim.GetCurrentAnimatorStateInfo ( 0 ).IsName ( AnimName ) )
{
return true;
}
return false;
}
public void MyAnimSetTrigger ( string AnimName )
{
if ( !IsPlayingAnim ( AnimName ) )
{
Anim.SetTrigger ( AnimName );
}
}
protected void MonsterFlip ( )
{
MonsterDirRight = !MonsterDirRight;
Vector3 thisScale = transform.localScale;
if ( MonsterDirRight )
{
thisScale.x = -Mathf.Abs ( thisScale.x );
}
else
{
thisScale.x = Mathf.Abs ( thisScale.x );
}
transform.localScale = thisScale;
rb.velocity = Vector2.zero;
}
//몬스터가 플레이어방향으로 향하고있는지 체크
protected bool IsPlayerDir ( )
{
if ( transform.position.x < PlayerData.Instance.Player.transform.position.x ? MonsterDirRight : !MonsterDirRight )
{
return true;
}
return false;
}
protected void GroundCheck ( )
{
if ( Physics2D.BoxCast ( boxCollider.bounds.center, boxCollider.size, 0, Vector2.down, 0.05f, layerMask ) )
{
isGround = true;
}
else
{
isGround = false;
}
}
public void TakeDamage ( int dam )
{
currentHp -= dam;
isHit = true;
// Knock Back or Dead
hitBoxCollider.SetActive ( false );
}
protected void OnTriggerEnter2D ( Collider2D collision )
{
//if ( collision.transform.CompareTag ( ?? ) )
//{
//TakeDamage ( 0 );
//}
}
}
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Mushroom : Monster { public Transform[] wallCheck; private void Awake ( ) //요부분에 Awake가 노란 밑줄이 그어집니다. 'Mushroom.Awake()'은(는) 상속된 'Monster.Awake()' 멤버를 숨깁니다. 숨기려면 new 키워드를 사용하세요. [Assembly-CSharp] 창뜸. { base.Awake ( ); moveSpeed = 2f; jumpPower = 15f; } // Update is called once per frame void Update ( ) { if ( !isHit ) { rb.velocity = new Vector2 ( -transform.localScale.x * moveSpeed, rb.velocity.y ); if ( !Physics2D.OverlapCircle ( wallCheck[0].position, 0.01f, layerMask ) && Physics2D.OverlapCircle ( wallCheck[1].position, 0.01f, layerMask ) && !Physics2D.Raycast ( transform.position, -transform.localScale.x * transform.right, 1f, layerMask ) ) { rb.velocity = new Vector2 ( rb.velocity.x, jumpPower ); } else if ( Physics2D.OverlapCircle ( wallCheck[1].position, 0.01f, layerMask ) ) { MonsterFlip ( ); } } } protected void OnTriggerEnter2D ( Collider2D collision ) //OnTriggerEnter2D도 노란밑줄됨 { base.OnTriggerEnter2D ( collision ); if ( collision.transform.CompareTag ( "PlayerHitBox" ) ) { MonsterFlip ( ); } } }
|
Monster 클래스의 Awake() 함수를 protected virtual void Awake()로 선언 Mushroom 클래스의 Awake() 함수를 protected override void Awake()로 선언
지금 이 코드는 Monster 클래스의 Awake()를 virtual로 선언하지 않았기 때문에 Mushroom 클래스 입장에서는 Awake()를 사용만 할 수 있지 오버라이딩 하는 것이 아니라 새로 정의하고 있는 거야. 따라서 Mushroom 클래스에서 그냥 Awake()를 사용하면 Mushroom 클래스의 Awake()를 사용하게 되고 부모 클래스인 Monster 클래스의 Awake()는 가려지게 되는 거지. 그래서 숨긴다고 에러가 뜨는 거야.
감사합니다. 그런데밑줄은 삭제가 되었는데 게임 플레이 버튼 누르면 오류가 있다고실행이 안되네요 다른 방법 찾아볼꼐요
공부할 키워드 : 다형성