Game Dev Study Log

by ricale

취미로 하는 게임 개발 공부

강의 31,32,33강 수강

강의 수강: Unity Turn-Based Strategy Game: Intermediate C# Coding

31. Cinemachine (06:18)

카메라 컨트롤을 위해 Cinemachine 패키지를 프로젝트에 설치

Cinemachine 의 Virtual Camera 게임 오브젝트를 프로젝트에 추가하면 Main Camera 객체와 연결된다. 이제 카메라 설정 및 조작은 Cinemachine 의 Virtual Camera 를 통해 한다.

Cinemachine Virtual Camera 설정

  • Follow: 카메라가 따라다닐 객체
  • Look at: 카메라가 바라볼 객체
  • Lens - Verical FOV: 화면 시야각
  • Body
    • Follow Offset: 따라다닐 객체와의 거리
    • X Damping, Y Damping, Z Dumping: 카메라 위치가 전환되는 속도
  • Aim
    • Horizontal Damping, Vertical Damping: 대상의 축 전환 시 카메라 전환 속도

32. Camera Move and Rotate (06:20)

    private void Update()
    {
        Vector3 inputMoveDir = new Vector3(0, 0, 0);
        if(Input.GetKey(KeyCode.W))
            inputMoveDir.z = +1f;
        if(Input.GetKey(KeyCode.S))
            inputMoveDir.z = -1f;
        if(Input.GetKey(KeyCode.A))
            inputMoveDir.x = -1f;
        if(Input.GetKey(KeyCode.D))
            inputMoveDir.x = +1f;

        float moveSpeed = 10f;
        Vector3 moveVector = transform.forward * inputMoveDir.z
            + transform.right * inputMoveDir.x;
        transform.position += moveSpeed * Time.deltaTime * moveVector;

        Vector3 rotationVector = new Vector3(0, 0, 0);
        if(Input.GetKey(KeyCode.Q))
            rotationVector.y = +1f;
        if(Input.GetKey(KeyCode.E))
            rotationVector.y = -1f;

        float rotationSpeed = 100f;
        transform.eulerAngles += rotationSpeed * Time.deltaTime * rotationVector;
    }

33. Camera Zoom (14:19)

강사는 줌 방식을 카메라의 followOfset 의 y 축을 조정하는 방식으로 구현한다. 이는 개인에 따라 원하는 줌 방식이 아닐 수 있으므로, 원하지 않을 경우 다른 구현 방법을 찾아보자.

Cinemachine 패키지가 followOffset 을 조정할 수 있는 친절한 인터페이스를 제공하지 않으므로, 일련의 과정을 통해 Cimemachine 내부에서 followOffset 을 관리하는 컴포넌트를 찾아낸 다음, 해당 컴포넌트를 통해 해당 값을 조정한다.

    float zoomAmount = 1f;
    if(Input.mouseScrollDelta.y > 0)
    {
        targetFollowOffest.y -= zoomAmount;
    }
    if(Input.mouseScrollDelta.y < 0)
    {
        targetFollowOffest.y += zoomAmount;
    }
    targetFollowOffest.y = Mathf.Clamp(targetFollowOffest.y, MIN_FOLLOW_Y_OFFSET, MAX_FOLLOW_Y_OFFSET);

    float zoomSpeed = 5f;
    cinemachineTransposer.m_FollowOffset = Vector3.Lerp(
        cinemachineTransposer.m_FollowOffset,
        targetFollowOffest,
        Time.deltaTime * zoomSpeed
    );

한 가지 주의할 것은, Lerp 함수의 설명은 "Linearly interpolates between two points." 이지만, 적용한 구현 상 실제로는 linear 하게 적용되지 않고 ease out 방식으로 적용될 것이다.