Written on
·
229
0
코드 상 문제가 없다고 나오긴 하는데, 어디가 잘못된 곳인지 못 찾겠습니다. 클릭하고 계속 마우스만 따라오는데 여기를 어떻게 고쳐야 할까요?
public class PlayerController : MonoBehaviour
{
[SerializeField]
float _speed = 10.0f;
bool _moveToDest = false;
Vector3 _destPos;
void Start()
{
Managers.Input.KeyAction -= OnkeyBoard;
Managers.Input.KeyAction += OnkeyBoard;
Managers.Input.MouseAction -= OnMouseClicked;
Managers.Input.MouseAction += OnMouseClicked;
}
void Update()
{
if (_moveToDest)
{
Vector3 dir = _destPos - transform.position;
if (dir .magnitude < 0.0001f)
{
_moveToDest = false;
}
else
{
float moveDist = Mathf.Clamp(_speed * Time.deltaTime, 0, dir.magnitude);
transform.position += dir.normalized * _speed * Time.deltaTime;
transform.LookAt(_destPos);
}
}
}
void OnkeyBoard()
{
//+-delta
//transform.Rotate(new Vector3(0.0f, Time.deltaTime * 100.0f, 0.0f));
//transform.rotation = Quaternion.Euler(new Vector3(0.0f, _yAngle, 0.0f));
if (Input.GetKey(KeyCode.W))
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.forward), 0.2f);
transform.position += Vector3.forward * Time.deltaTime * _speed;
}
if (Input.GetKey(KeyCode.S))
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.back), 0.2f);
transform.position += Vector3.back * Time.deltaTime * _speed;
}
if (Input.GetKey(KeyCode.A))
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.left), 0.2f);
transform.position += Vector3.left * Time.deltaTime * _speed;
}
if (Input.GetKey(KeyCode.D))
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.right), 0.2f);
transform.position += Vector3.right * Time.deltaTime * _speed;
}
_moveToDest = false;
}
void OnMouseClicked(Define.MouseEvent evt)
{
if (evt != Define.MouseEvent.Click)
return;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(Camera.main.transform.position, ray.direction * 100.0f, Color.red, 1.0f);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100.0f, LayerMask.GetMask("Wall")))
{
_destPos = hit.point;
_moveToDest = true;
//Debug.Log($"Raycast Camera @ {hit.collider.gameObject.tag}");
}
}
}
Answer 2
0
if (_pressed)
MouseAction.Invoke(Define.MouseEvent.Click);
(추가됨) _pressed = false;
transform.position += dir.normalized * _speed * Time.deltaTime;
-----------
transform.position += dir.normalized * moveDist;
(수정됨)
코딩의 문제가 있었네요 ㅎㅎ;;
0
버그가 발생하면 코드로 보기보다는
디버깅을 통해서 잡는 연습이 필요합니다.
특히 유니티 등 상용엔진의 경우
코드 외에도 에셋 환경 등 여러가지가 맞물려 돌아가기 때문에 더욱 그렇습니다.
이동이 멈추지 않는 부분이라면, 아무래도
if (dir .magnitude < 0.0001f)
_moveToDest = false;
위 부분에 들어오는게 맞는지 BreakPoint를 잡아 실행해보시기 바랍니다.
그리고 중간 중간 강의 코드가 업로드 되었으니,
파일 단위로 복붙 교체해보면서 혹~시 다른 부분이 없는지 확인해보는 것도 도움이 됩니다.
(책을 보면서 오동작할 때 제가 종종 사용하는 방법입니다)
고민해봐도 문제 원인 파악이 힘드시다면
rookiss@naver.com로 전체 프로젝트 압축 후 보내주시기 바랍니다.