728x90
728x170

마우스 움직임에 따라 카메라 시점을 이동하는 방법입니다.
아래 Script를 Main Camera에 Add 하면 됩니다.
마우스 움직임을 Input.GetAxis로 X, Y 값을 가져와 카메라의 eulerAngles을 변경하여 시점을 이동합니다.
또한 Mathf.Clamp를 이용하여 하늘, 땅을 보았을 때 -90~90 도 제한을 걸어서 한 바퀴 돌지 않도록 합니다.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CamRotation : MonoBehaviour
{
    // 회전속도
    public float rotSpeed = 200f;
    // 회전값 변수
    private float mx = 0;
    private float my = 0;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        float x = Input.GetAxis("Mouse X");
        float y = Input.GetAxis("Mouse Y");

        mx += x * rotSpeed * Time.deltaTime;
        my += y * rotSpeed * Time.deltaTime;

        // my 값을 -90 에서 90 까지로 제한하기
        my = Mathf.Clamp(my, -90, 90);

        transform.eulerAngles = new Vector3(-my, mx, 0);
    }
}

결과

728x90
그리드형
Posted by kjun
,