관리 메뉴

도드넷

게임 개발 일지#9 - 유니티 2D 미사일 투사체 원거리 공격! 본문

창고/게임 제작 [시즌2]

게임 개발 일지#9 - 유니티 2D 미사일 투사체 원거리 공격!

도드! 2016. 10. 10. 15:28
반응형




게임 개발 일지#9 - 유니티 2D 미사일 투사체 원거리 공격!


#1 두점을 기준으로 스프라이트 회전시키기


            float AngleRad = Mathf.Atan2(Shoot_Point.y - This_Projectile.transform.position.y, Shoot_Point.x - This_Projectile.transform.position.x);       
            float AngleDeg = (180 / Mathf.PI) * AngleRad;        
            this.transform.rotation = Quaternion.Euler(0, 0, AngleDeg);


두점 Shoot_Point 와 프로젝타일의 현재위치(This_Projectile.transform.position)를 기준으로 미사일

스프라이트를 회전시킴.


막 투사체가 거꾸로 회전하거나 하는건 그냥 스프라이트나 모델 수정하는 방법 밖에 없음



회전의 중심은 화살촉의 뒤로가게 미사일 스프라이트를 구성해야함.


#2 투사체 속도와 방향 설정하기


            float X_diff = Mathf.Abs(This_Transform.position.x - Shoot_Point.x);
            float Y_diff = Mathf.Abs(This_Transform.position.y - Shoot_Point.y);
            float Diff_Ratio;

            // 차잇값에 따른 비율 계산
            if (X_diff >= Y_diff)
            {
                Diff_Ratio = Y_diff / X_diff;
            }
            else
            {
                Diff_Ratio = X_diff / Y_diff;
            }

            // MOVE 목적지로 이동
            if (This_Transform.position.x - Shoot_Point.x < 0.05)
            {
                if (X_diff < Y_diff)
                {
                    X_Speed = Speed * Diff_Ratio;
                }
                else
                {
                    X_Speed = Speed;
                }

            }
            else if (This_Transform.position.x - Shoot_Point.x > 0.05)
            {
                if (X_diff < Y_diff)
                {
                    X_Speed = -Speed * Diff_Ratio;
                }
                else
                {
                    X_Speed = -Speed;
                }

            }

            if (This_Transform.position.y - Shoot_Point.y < 0.05)
            {
                if (X_diff > Y_diff)
                {
                    Y_Speed = Speed * Diff_Ratio;
                }
                else
                {
                    Y_Speed = Speed;
                }

            }
            else if (This_Transform.position.y - Shoot_Point.y > 0.05)
            {
                if (X_diff > Y_diff)
                {
                    Y_Speed = -Speed * Diff_Ratio;
                }
                else
                {
                    Y_Speed = -Speed;
                }

            }


사실상 전에 사용했던 플레이어 캐릭터 속도설정및 이동 스크립트와 다른점 없음.


X,Y 차잇값 구해서 1이하의 비율 구한 다음 작은쪽에 곱해서 고정 속도를 줄여주는 방식.


#3 투사체 충돌검출


    void OnTriggerEnter2D(Collider2D thiscoll)
    {
        if (thiscoll.gameObject.tag == "HitBox" && thiscoll.gameObject.transform.parent.gameObject == Target)
        {
            Destroy(gameObject);
        }
    }


원거리 투사체는 힛박스라는 태그된 콜라이더와 충돌 + 충돌 물체가 타겟과 같을경우 증발하게 만듬.


딜넣는건 데미지 핸들러에 데미지 넣어주면 될일.


#4 유니티 미사일 투사체 생성


    void OnFire()
    {
        if (Target)
        {
            GameObject PJ = Instantiate(Prefab_Projectile) as GameObject;
            PJ.GetComponent<Transform>().position = Fire_Point.GetComponent<Transform>().position; // 미사일 위치설정!
            PJ.GetComponent<Projectile>().Shoot_Point = Target.GetComponent<Stats>().Missles_Come_Here.GetComponent<Transform>().position; // 모델정중앙을 슛포인트로 잡는다.
            PJ.GetComponent<Projectile>().Target = Target; // 충돌을 일으킬 타겟을 정한다.
            PJ.GetComponent<Projectile>().Fly = true;
        }    
    }


Instantiate() as GameObject 라는 프리뺍 기준으로 복사체를 만드는 함수를 사용.

이후 위치설정, 미사일에 이동방향, 대상정보 전달하고 Fly 온 해서 날아갈수있게 해줌.






반응형
Comments