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);
}
이렇게 수정했음
콜라이더 충돌하면 픽셀 단위에서 어긋날 수 있어서 최종적으로 수정하면
잘 됨! (픽셀 단위가 1 / 16 = 0.0625)
움직임을 스내핑하면 점프나 복잡한 움직임 구현이 힘들던데
지금은 점프나 복잡한 움직임이 들어갈 계획이 없어서 스내핑 방식을 써도 괜찮을 것 같습니다. 나중에 기능이 변경되어 그런 움직임을 추가해야 하면, 말씀 주신 대로 스내핑 방식을 빼거나 조정할 수 있겠네요. 좋은 의견 감사합니다!
유니티 픽셀 단위로 움직이게 하는 거 뭐보고 배웠나요?
코어키퍼에서 픽셀 단위로 움직이는 거 보고 참고해 봤습니다.
그렇군요. 알려줘서 감사합니다.