private Rigidbody2D rb; private Vector2 nextVector = Vector2.zero; private float speed = 5f; public void Move(Vector2 inputVector) { // 입력 벡터가 바뀌었을 때만 속도 계산 if (inputVector != Vector2.zero) { nextVector = speed * Time.fixedDeltaTime * inputVector; } rb.MovePosition(rb.position + nextVector); }
원래 Move 함수의 코드.


여기서, 픽셀 단위로 움직이게 하고 싶어서


private Rigidbody2D rb; private Vector2 nextVector = Vector2.zero; private float speed = 5f; private int pixelsPerUnit = 16; private void Move(Vector2 direction) { Vector2 currentPos = rb.transform.position; Vector2 targetPos; if (direction.magnitude > 0) { targetPos = currentPos + direction / pixelsPerUnit * speed; } else { // 멈출 때 픽셀 정렬 (최종 위치를 픽셀 단위로) float targetX = Mathf.Round(currentPos.x * pixelsPerUnit) / pixelsPerUnit; float targetY = Mathf.Round(currentPos.y * pixelsPerUnit) / pixelsPerUnit; targetPos = new Vector2(targetX, targetY); } rb.MovePosition(targetPos); }

이렇게 수정했음

콜라이더 충돌하면 픽셀 단위에서 어긋날 수 있어서 최종적으로 수정하면


7fed8273a8826beb3ee996e44e9c7565e94394b2abe103ba4286fbe288ae5627c2ea89


잘 됨! (픽셀 단위가 1 / 16 = 0.0625)