유니티 2D 돌진 - yuniti 2D doljin

void lookPlayer()

{

DIRECTION = (player.GetComponent<Transform>().position.x < transform.position.x ? -1 : 1); //player와 자신(보스)의 x좌표를 비교해서 적당한 상수를 DIRECTION에 저장한다.

float scale = transform.localScale.z;

transform.localScale = new Vector3(DIRECTION * -1 * scale, scale, scale); //DIRECTION변수를 이용해서 player쪽을 바라보도록한다.

}

IEnumerator rush()

{

animator.SetTrigger("ready"); //돌진을 준비하는 animation

lookPlayer();//player 쪽을 보게한다.

yield return new WaitForSeconds(1);

animator.SetTrigger("rushing"); //돌진하는 animation

bool isBroken = false;

while (!isBroken)

{

yield return new WaitForSeconds(0.1f);

if (speedx > rigid2D.velocity.x * DIRECTION) //AddForce를 이용해서 자연스럽게 움직이도록 하되 speedx보다 빠르지 않도록한다.

{

rigid2D.AddForce(transform.right * DIRECTION * 1000);

}

pos = new Vector2(transform.position.x + DIRECTION * 5, transform.position.y);

boxSize = new Vector2(12);

Collider2D[] collider2Ds = Physics2D.OverlapBoxAll(pos, boxSize, 0);//자신(보스) 앞쪽에 있는 모든 충돌체들을 저장한다.

foreach (Collider2D collider in collider2Ds)

{

if (collider != this.GetComponent<CapsuleCollider2D>() && (collider.tag == "Ground")) //충돌체가 자신(보스)의 충돌체가 아니고 tag가 Ground일 때 멈추도록한다.

{

rigid2D.velocity = new Vector2(0, rigid2D.velocity.y);

isBroken = true;

break;

}

}

}

animator.SetBool("stunned"true); //부딪혀서 스턴 당한 animation

StartCoroutine("stunCounter"); //4초후에 stunned를 false로 설정하는 coroutine을 호출한다.

yield return new WaitForSeconds(1.5f);

this.glopGenerator_1.GetComponent<GlopGenerator>().summonGlop(); //Glop(몬스터를 소환한다.)

this.glopGenerator_2.GetComponent<GlopGenerator>().summonGlop();

this.glopGenerator_3.GetComponent<GlopGenerator>().summonGlop();

nextPattern = EAT; // 연계를 위해서 

yield return new WaitForSeconds(3);

nextPatternPlay(); //다음 패턴을 실행한다.

}

IEnumerator eat()

{

animator.SetTrigger("ready");

lookPlayer();

yield return new WaitForSeconds(1);

animator.SetTrigger("eating");

this.isCanDamaged = true;

int eatCount = 0;

bool isBroken = false;

while (!isBroken)

{

yield return new WaitForSeconds(0.1f);

if (speedx > rigid2D.velocity.x * DIRECTION)

{

rigid2D.AddForce(transform.right * DIRECTION * 1000);

}

pos = new Vector2(transform.position.x + DIRECTION * 5, transform.position.y);

boxSize = new Vector2(12);

Collider2D[] collider2Ds = Physics2D.OverlapBoxAll(pos, boxSize, 0);

foreach (Collider2D collider in collider2Ds)

{

if (collider.gameObject != gameObject && (collider.tag == "Ground"))

{

isBroken = true;

break;

}

else if (collider.gameObject != gameObject && collider.tag == "Villain"//tag가 villain일 때 즉 아까 소환한 Glop(몬스터)일 때 그것을 파괴하고 그 숫자를 센다.

{

Destroy(collider.gameObject);

eatCount += 1;

}

}

}

animator.SetBool("stunned"true);

StartCoroutine("stunCounter");

this.isCanDamaged = false;

amountOfBone = eatCount * 3//먹은(파괴한) Glop(몬스터)의 수에 따라서 뼈의 수를 정한다.

if (amountOfBone == 0//뼈가 없을 때는 랜텀으로 다음 패턴을 정한다.

{

if (0 == Random.Range(02)) nextPattern = RUSH;

else nextPattern = JUMP;

}

else //뼈가 있을 때는 연계를 위해서 강제로 다음 패턴을 정한다.

{

nextPattern = SPIT;

}

yield return new WaitForSeconds(3);

nextPatternPlay();

}

IEnumerator stunCounter() //위에서 호출되는 함수로 4초가 지난 후에 stunned를 false로 설정한다.

{

yield return new WaitForSeconds(4);

animator.SetBool("stunned"false);

}