프로그래밍 / C++ / 언리얼

Programming/C# | Unity

[Unity] 1인칭 마우스 입력 / 카메라 회전 & Zoom 기능 구현하기.

아트성 2022. 8. 15. 23:43

카메라 회전

 

 

 

RotateToMouse.cs

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

public class RotateToMouse : MonoBehaviour
{
    [SerializeField] private float rotCamXAxisSpeed = 5f; // 카메라 x축 회전속도
    [SerializeField] private float rotCamYAxisSpeed = 3f; // 카메라 y축 회전속도

    private float limitMinX = -80; // 카메라 x축 회전 범위 (최소)
    private float limitMaxX = 50; // 카메라 x축 회전 범위 (최대)

    private float eulerAngleX; // 마우스 좌 / 우 이동으로 카메라 y축 회전
    private float eulerAngleY; // 마우스 위 / 아래 이동으로 카메라 x축 회전

    public void CalculateRotation(float mouseX, float mouseY)
    {
        eulerAngleY += mouseX * rotCamYAxisSpeed; 
        eulerAngleX -= mouseY * rotCamYAxisSpeed; 
        eulerAngleX = ClampAngle(eulerAngleX, limitMinX, limitMaxX);
        transform.rotation = Quaternion.Euler(eulerAngleX, eulerAngleY, 0);
    }

    // 카메라 x축 회전의 경우 회전 범위를 설정
    private float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360)
        {
            angle += 360;
        }

        if (angle > 360)
        {
            angle -= 360;
        }

        return Mathf.Clamp(angle, min, max);
    }
}

playerController.cs

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

public class PlayerController : MonoBehaviour
{
    private RotateToMouse rotateToMouse; // 마우스 이동으로 카메라 회전

    void Awake()
    {
        rotateToMouse = GetComponent<RotateToMouse>();
    }

    void Update()
    {
        UpdateRotate();
    }

    void UpdateRotate()
    {
        float mouseX = Input.GetAxis("Mouse X");
        float mouseY = Input.GetAxis("Mouse Y");
        rotateToMouse.CalculateRotation(mouseX, mouseY);
    }
}

 

 

 

 

 

카메라 Zoom IN / OUT

 

Zoom.cs

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

public class Zoom : MonoBehaviour
{
    [SerializeField] private float zoomSpeed = 0f;
    [SerializeField] private float zoomMax = 5f;
    [SerializeField] private float zoomMin = 0.8f;

    PlayerController player;

    private void Awake()
    {
        player = GetComponentInParent<PlayerController>();
    }

    public void ZoomInOut(float zoomDirection)
    {
        float zoom = Vector3.Distance(transform.position, player.transform.position);
        zoom = Mathf.Clamp(zoom, zoomMin, zoomMax);

        if (zoom >= zoomMax && zoomDirection > 0) return;
        if (zoom <= zoomMin && zoomDirection < 0) return;
        
        transform.position += transform.forward * zoomDirection * zoomSpeed;
    }
}

 

playerController.cs

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

public class PlayerController : MonoBehaviour
{
    private RotateToMouse rotateToMouse; // 마우스 이동으로 카메라 회전
    private Zoom zoom;

    void Awake()
    {
        rotateToMouse = GetComponent<RotateToMouse>();
        zoom = GetComponentInChildren<Zoom>();
    }

    void Update()
    {
        UpdateRotate();
        UpdateZoom();
    }

    void UpdateRotate()
    {
        float mouseX = Input.GetAxis("Mouse X");
        float mouseY = Input.GetAxis("Mouse Y");
        rotateToMouse.CalculateRotation(mouseX, mouseY);
    }

    void UpdateZoom()
    {
        float t_zoomDirection = Input.GetAxis("Mouse ScrollWheel");
        zoom.ZoomInOut(t_zoomDirection);
    }
}

 

반응형