유니티 2D에서 자연스러운 투사체를 구현해보자잇!

박뚝딱·2020년 6월 8일
4
post-thumbnail

유니티2D Isometric View에서 자연스러운 투사체를 구현하는 방법을 구상하다가 베지어 커브를 사용하게 되었습니다.

공식은 s = 1 - t 일때
A = (s * P0) + (t * P1)
B = (s * P1) + (t * P2)
C = (s * A) + (t * B)

입니다.

구현하기 귀찮으므로 유니티에서 지원하는 AnimationCurve를 사용하겠습니다.


클래스 멤버에 AnimationCurve 변수를 추가합니다.
그 다음 추가한 컴포넌트가 있는 인스펙터로 돌아와서 curve를 수정했습니다.

그다음 함수를 하나 만들어 줬습니다.

    private IEnumerator IEFlight()
    {
        float duration = flightSpeed;
        float time = 0.0f;
        Vector3 start = transform.position;
        Vector3 end = target.transform.position

        while(time < duration)
        {
            time += Time.deltaTime;
            float linearT = time / duration;
            float heightT = curve.Evaluate(linearT);

            float height = Mathf.Lerp(0.0f, hoverHeight, heightT);

            transform.position = Vector2.Lerp(start, end, linearT) + new Vector2(0.0f, height);

            yield return null;
        }
        StartCoroutine("IEDestroy");
    }

투사체의 위치가 타겟의 위치로 선형보간하고, curve에 선형보간한 값을 더해주면 썸네일과 같은 투사체가 완성됩니다.

profile
뚝딱!

0개의 댓글