728x90

 마우스 우클릭으로 활을 조준하고 조준점으로 플레이어 회전을 하는 코드를 구현하던중 

플레이어가 조준점을 바라보다가 움직이면 이상한곳으로 심하게 흔들리듯 회전하는 현장이 발생했습니다

특정 방향으로 움직일때 플레이어가 이상하게 돌아가는 모습


Debug.Log(hit.transform.name);

 

혹시하는 마음에 Debug.Log를 출력해보았고 

역시 플레이어가 찍히고있었습니다...

 

    ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit, 1000))
        {
        // hit지점의 x,y축만 받아와서 플레이어가 수직으로 회전하는것을 방지
            Vector3 lookAtPoint = new Vector3(hit.point.x,transform.position.y, hit.point.z);
            transform.LookAt(lookAtPoint);
        }

화면 중심에서 Ray를 쏘고 플레이어가 hit 지점을 바라보도록 했습니다 

 

문제원인

 카메라에서부터 Ray를 쏘기때문에 카메라로부터 Ray를 쏴서 플레이어와 충돌하면 hit.Point가 자기 자신이기 때문에 회전에 이상이 생겼던 것입니다. 


해결방법

    int layerMask = ~LayerMask.GetMask("Player");
    
    private void RayToCenter()
    {
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit, 1000, layerMask))
        {
            Vector3 lookAtPoint = new Vector3(hit.point.x, Movement.instance.transform.position.y, hit.point.z);
            Movement.instance.transform.LookAt(lookAtPoint);
        }
    }


플레이어를 제외한 모든 레이어를 받아오고 

지정한 레이어만 Ray가 충돌하도록 해서 플레이어와 충돌하지 못하도록 만들었습니다.

 


결과

플레이어가 정상적으로 조준점을 바라보는 모습을 볼 수 있습니다.

 

728x90

+ Recent posts