1. 화살이 부딫힌 오브젝트가 합당하면 (플랫폼/적) 실행된다

2. SetParent로 인한 scale 변경을 방지하기 위해 임시 부모를 생성한다

3. 임시 부모를 목표 부모(충돌체)에 자식으로 넣는다

4. stickArrow를 생성한다 (사용할 화살과 동일해 보이지만, script와 rigidBody나 collider가 제거된 오브젝트)

5. stickArrow를 임시 부모의 자식으로 넣는다

6. 2초 후 제거한다

(충돌 당시 방향으로 조금 이동시킨 후 작업하면 화살촉이 진짜로 박혀있는 듯 보일듯함)

private void OnCollisionEnter2D(Collision2D other)
{
    GameObject otherObject = other.gameObject;

    if (isArrow && (otherObject.CompareTag("Platform") || otherObject.CompareTag("Enemy")))
    {
        // scale 변경 방지용 쿠션 parent
        GameObject sharedParent = new GameObject("Father");
        sharedParent.transform.position = otherObject.transform.position;
        sharedParent.transform.rotation = otherObject.transform.rotation;
        sharedParent.transform.SetParent(otherObject.gameObject.transform);

        // 고정될 화살 생성
        GameObject newArrow = Instantiate(stickArrow, transform.position, transform.rotation);
        newArrow.transform.SetParent(sharedParent.transform, true);
        //2초 후 소멸
        Destroy(newArrow, 2);
    }
}

 

 

 

벽에 충돌해도 2초간 잘 남아있다

적에게 충돌하면, 2초간 적이 이동해도 따라 이동하며 남아있다 

+ Recent posts