본문 바로가기
개발/C#

유니티 C# 카메라 자유롭게 자유시점으로 이동하기 간단 구현

by SPNK 2024. 2. 14.
반응형
  • 코드 작성
using UnityEngine;

public class FreeCameraMovement : MonoBehaviour
{
    public float movementSpeed = 5f;
    public float rotationSpeed = 2f;

    void Update()
    {
        // 카메라 이동
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");

        Vector3 moveDirection = new Vector3(horizontalInput, 0f, verticalInput).normalized;
        Vector3 moveAmount = moveDirection * movementSpeed * Time.deltaTime;
        transform.Translate(moveAmount);

        // 카메라 회전
        if (Input.GetMouseButton(1)) // 오른쪽 마우스 버튼이 눌려 있을 때
        {
            float mouseX = Input.GetAxis("Mouse X");
            float mouseY = Input.GetAxis("Mouse Y");

            Vector3 rotationAmount = new Vector3(-mouseY, mouseX, 0f) * rotationSpeed;
            transform.Rotate(rotationAmount);
        }
    }
}
반응형

댓글