2D 캐릭터에 대한 이동조작

void CharacterMove ()
    {
        Vector2 _input = Vector2.zero;
        float _speed = 3.0f;
        float _move = 0.0f;
        float _down = 0.0f;

        _input.x = Input.GetAxisRaw("Horizontal");
        _input.y = Input.GetAxisRaw("Vertical");
        
        float _speed = m_data.m_speed[(int)_down];
         switch (_input)
        {
            case Vector2 v when v.Equals(Vector2.zero):
                break;
            default:
                _move = _input.x == 0.0f ? 0.0f : 1.0f;
                _down = _input.y < 0.0f ? 1.0f : 0.0f;
                if (!m_rollFlag && _move != 0.0f) transform.localScale = Gmanager.Instance.SetFlip(_input, ref m_flipFlag);
                break;
        }
        transform.Translate(_speed * _input.x * Time.deltaTime * Vector2.right);        
    }

변수 _move와 _down은 각각 이동상태 체크와 서있는지 앉아있는지 여부를 체크합니다

상태에 따른 캐릭터의 애니메이션 변경과  캐릭터 방향에 따른 플립에 사용됩니다.

Gmanager클래스는 따로 만들어준 클래스입니다.

 public Vector3 SetFlip(Vector2 argInput, ref bool argFlipflag)
    {// ref <-- C의 포인터 임
        Vector3 _scale = Vector3.one;

        switch ((int)argInput.x)
        {
            case -1:
                argFlipflag = true;
                break;
            case 1:
                argFlipflag = false;
                break;
        }

        _scale.x = argInput.x;

        return _scale;
    }

Gmanager클래스에 포함되어있는 SetFlip함수입니다.

'Unity' 카테고리의 다른 글

[Unity] SerializeField ? 직렬화 ?  (0) 2024.01.22
[Unity C#] Vector3  (0) 2024.01.22
2024-01-10  (0) 2024.01.10
2024_01_08  (0) 2024.01.08
2024-01-04// 옵셔널 체이닝, Rect Transform, Bounds  (0) 2024.01.04

+ Recent posts