๐Ÿ™‡โ€โ™€๏ธPlayer ์ด๋™

player ์ด๋™์„ if(Input.GetKey.W) //transform.position += Vector3.up * Time.deltaTime * _speed;๋กœ ํ•˜๋ฉด ๋‹ค๋ฅธ ๊ฐ์ฒด๊ฐ€ ์›€์ง์ผ ๋•Œ ์–ด๋ ค์›€์ด ์ƒ๊ธด๋‹ค

์ด๋ ‡๊ฒŒ ํ•˜์ง€๋ง๋ž˜ ์ž…๋ ฅ๊ณผ ์›€์ง์ž„์„ ํ•œ์ฝ”๋“œ์— ๋„ฃ์œผ๋ฉด ์•ˆ๋˜๋Š” ๋“ฏ ํ•จ

๐ŸชPlayer ์ด๋™

Define์— public enum MoveDir๋กœ ์ด๋™ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚˜๊ฒŒ ๋งŒ๋“ค์–ด ์คŒ

์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“œ๋Š” ๊ฒƒ์€ ์…€๋‹จ์œ„๋กœ ์ด๋™์ด๋ฏ€๋กœ grid๋ฅผ ๊ฐ€์ ธ์™€์•ผ ๋จ

์…€์ขŒํ‘œ์™€ ๊ทธ๋ฆฌ๋“œ์— ๋งž๊ฒŒ ์„ค์ • ํ•ด์ค˜์•ผ ๋จ

ํ‚ค๋ณด๋“œ ์ž…๋ ฅ์— ๋”ฐ๋ฅธ ์ƒํƒœ ๋ณ€ํ™” ํ•จ์ˆ˜

// ํ‚ค๋ณด๋“œ ์ž…๋ ฅ
void GetDirInput()
{
    if (Input.GetKey(KeyCode.W))
    {
        _dir = MoveDir.Up;
    }
    else if (Input.GetKey(KeyCode.S))
    {
        _dir = MoveDir.Down;
    }
    else if (Input.GetKey(KeyCode.A))
    {
        _dir = MoveDir.Left;
    }
    else if (Input.GetKey(KeyCode.D))
    {
        _dir = MoveDir.Right;
    }
    else
    {
        _dir = MoveDir.None;
    }
}

๋ฐฉํ–ฅ๋ฒกํ„ฐ๋ฅผ ๊ตฌํ•ด์„œ ์‹ค์ œ ์ด๋™ ์ค‘์ด๋ฉด ์Šค๋ฅด๋ฅต ์ด๋™ํ•˜๊ฒŒํ•˜๋Š” ํ•จ์ˆ˜

// ์Šค๋ฅด๋ฅต ์ด๋™ ์ฒ˜๋ฆฌ
void UpdatePosition()
{
    if (_isMoving == false)
        return;

    Vector3 desPos = _grid.CellToWorld(_cellPos) + new Vector3(0.5f, 0.5f);
    Vector3 moveDir = desPos - transform.position;

    // ๋„์ฐฉ ์—ฌ๋ถ€ ์ฒดํฌ
    float dist = moveDir.magnitude;
    if (dist < _speed * Time.deltaTime)
    {
        transform.position = desPos;
        _isMoving = false;
    }
    else
    {
        transform.position += moveDir.normalized * Time.deltaTime * _speed;
        _isMoving = true;
    }
}

์ด ํ•จ์ˆ˜์—์„œ Vector3 desPos = _grid.CellToWorld(_cellPos) + new Vector3(0.5f, 0.5f); Vector3 moveDir = desPos - transform.position; ์ด ๋ถ€๋ถ„์ด ์…€์ขŒํ‘œ์™€ ๊ทธ๋ฆฌ๋“œ๋ฅผ ๋งž์ถฐ์ฃผ๊ณ ์žˆ๊ณ  ๋ฐฉํ–ฅ๋ฒกํ„ฐ๋ฅผ ๊ตฌํ•œ๋‹ค

๊ทธ ๋ฐ‘์— ๋„์ฐฉ ์—ฌ๋ถ€ ์ฒดํฌ์—์„œ๋Š” ๋ฐฉํ–ฅ๋ฒกํ„ฐ์˜ ํฌ๊ธฐ ์ฆ, ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•ด์„œ ์ผ์ • ์ด์ƒ์˜ ์†๋„๊ฐ€ ๋‚˜์˜ค๋ฉด ์‹ค์ œ๋กœ ์Šค๋ฅด๋ฅต ์ด๋™ํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค

์‹ค์ œ ์ด๋™

// ์ด๋™ ๊ฐ€๋Šฅํ•œ ์ƒํƒœ์ผ ๋•Œ, ์‹ค์ œ ์ขŒํ‘œ๋ฅผ ์ด๋™ํ•œ๋‹ค
void UpdateIsMoving()
{
    if (_isMoving == false)
    {
        switch (_dir)
        {
            case MoveDir.Up:
                _cellPos += Vector3Int.up;
                _isMoving = true;
                break;
            case MoveDir.Down:
                _cellPos += Vector3Int.down;
                _isMoving = true;
                break;
            case MoveDir.Left:
                _cellPos += Vector3Int.left;
                _isMoving = true;
                break;
            case MoveDir.Right:
                _cellPos += Vector3Int.right;
                _isMoving = true;
                break;
        }
    }
}

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: