마블 VR 프로젝트 제작

[마블 VR] 방패 던지기 회전 구현

노재두내 2023. 12. 13. 16:03

구현하고 싶은 모습 

방패의 y축이 적의 방향을 바라보고, z축이 위쪽으로 향하도록 하고싶다.

 

먼저 y축이 적의 방향을 바라보도록 코드를 작성

ar dir = (nextEnemy.transform.position - this.shield.transform.position).normalized;
            this.shield.transform.up = dir;

 

결과

잘 바라보지만 z축이 위쪽이 아닌 다른쪽을 바라본다.

 

위 상태에서 y축으로 몇도 회전 해야 방패의 윗부분이 위쪽으로 오는지 확인

방패를 기준으로 방패의 오른쪽에 적이 있으면 270도 , 왼쪽에 있으면 90 회전하면 위쪽을 바라본다.

이를 바탕으로 코드 작성

 private void RotateShield(GameObject target)
    {
        if (target != null)
        {
            DOTween.Kill("rotateTween");
            //적 쳐다보기
            var dir = (target.transform.position - this.transform.position).normalized;
            this.transform.up = dir;
            
            if (this.transform.position.x < target.transform.position.x)
            {
                degrees = 270;
                Debug.LogFormat("<color=yellow>오른쪽에 있음 : {0}</color>", degrees);

            }
            else if (this.transform.position.x > target.transform.position.x)
            {
                degrees = 90;
                Debug.LogFormat("<color=yellow>왼쪽에 있음 : {0}</color>", degrees);
            }
            else
            {
                Debug.LogFormat("<color=yellow>같은 위치에 있음 : {0}</color>", degrees);
            }
            this.transform.rotation = this.transform.rotation * Quaternion.Euler(0, degrees, 0);
        }
        else if (target == null)
        {
            this.Dorotate();
        }
        
    }

 

실행결과


날라갈때랑 돌아올 때도 방패가 위쪽을 바라보도록 하기

 

전에 두트윈으로 구현해놨던 Dorotate를 활용하고 싶음

하지만 Dorotate를 적용하면 적을 바라보면 공격하는 부분에서 적을 바라보는게 적용이 안됨

 

그래서 멈추는 방법을 찾았는데 DOTween.Kill이라는게 있었다.

public void Dorotate()
    {
        this.transform.DORotate(new Vector3(-90, -83, 0), 1f).SetId("rotateTween");
    }
    
    
 private void RotateShield(GameObject target)
    {
        if (target != null)
        {
            DOTween.Kill("rotateTween");
            //적 쳐다보기
            var dir = (target.transform.position - this.transform.position).normalized;
            this.transform.up = dir;
            
            if (this.transform.position.x < target.transform.position.x)
            {
                degrees = 270;
                Debug.LogFormat("<color=yellow>오른쪽에 있음 : {0}</color>", degrees);

            }
            else if (this.transform.position.x > target.transform.position.x)
            {
                degrees = 90;
                Debug.LogFormat("<color=yellow>왼쪽에 있음 : {0}</color>", degrees);
            }
            else
            {
                Debug.LogFormat("<color=yellow>같은 위치에 있음 : {0}</color>", degrees);
            }
            this.transform.rotation = this.transform.rotation * Quaternion.Euler(0, degrees, 0);
        }
        else if (target == null)
        {
            this.Dorotate();
        }
        
    }


버그 수정.손에 붙을 때 rotation값을 Init을 해줬는데 이상하게 붙는경우

짧게 던지고 잡는 경우에 이런 경우가 있었음

 

==> 이것도 dorotate가 멈추지 않고 계속 실행되고 있어서라는 생각이 듬 , 

 public void Init()
    {
        Debug.Log("위치 초기화 완료");
        this.pos.SetParent(lHandAnchor);
        this.pos.localPosition = new Vector3(0, 0, 0);
        this.pos.localRotation = Quaternion.Euler(0, 0, 0);
        this.transform.localRotation=Quaternion.Euler(0, -83, 0);
        DOTween.Kill("rotateTween");
    }

초기화 할때도 kii을 해주니 제대로 동작한다.