UNITY/Unity Study

카메라 컨트롤

GREEN나무 2023. 7. 5. 16:54
728x90
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SempleCamera : MonoBehaviour
{
    [SerializeField] Transform followTarget;
    [SerializeField] float distance = 5;

    float rotationY;
    private void Update()
    {
                                                        // 회전                       카메랑와의 거리
        transform.position = followTarget.position - Quaternion.Euler(0, 45, 0)  * new Vector3(0, 0, distance);
    }
}

카메라와 플레이어 사이의 거리 , 각도 지정하기

 

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

public class SempleCamera : MonoBehaviour
{
    [SerializeField] Transform followTarget;
    [SerializeField] float distance = 5;

    float rotationY;
    private void Update()
    {
        // 마우스로 카메라 회전
        rotationY +=Input.GetAxis("Mouse X");

        var targetRotation = Quaternion.Euler(0, rotationY, 0);

                                                        // 회전           카메라와 플레이어 사이의 거리
        transform.position = followTarget.position - targetRotation  * new Vector3(0, 0, distance);
        transform.rotation = targetRotation;

    }
}

마우스로 카메라 회전시키기(플레이어 주위를 돌음) :  Y축 기준 = 좌우로

 

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

public class SempleCamera : MonoBehaviour
{
    [SerializeField] Transform followTarget;
    [SerializeField] float distance = 5;

    float rotationX;
    float rotationY;
    private void Update()
    {

        // 마우스로 카메라 회전
        rotationX += Input.GetAxis("Mouse Y"); // 위 아래
        rotationY += Input.GetAxis("Mouse X"); // 좌우


        var targetRotation = Quaternion.Euler(rotationX, rotationY, 0);

                                                        // 회전           카메라와 플레이어 사이의 거리
        transform.position = followTarget.position - targetRotation  * new Vector3(0, 0, distance);
        transform.rotation = targetRotation;

    }
}

 

상하, 좌우 

 

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

public class SempleCamera : MonoBehaviour
{
    [SerializeField] Transform followTarget;
    [SerializeField] float distance = 5;

    // 카메라 위아래 회전각 제한걸기
    [SerializeField] float minVerticakAngle = -45;
    [SerializeField] float maxVerticakAngle = 45;

    float rotationX;
    float rotationY;
    private void Update()
    {

        // 마우스로 카메라 회전
        rotationX += Input.GetAxis("Mouse Y"); // 위 아래
        rotationX = Mathf.Clamp(rotationX, minVerticakAngle, maxVerticakAngle);

        rotationY += Input.GetAxis("Mouse X"); // 좌우


        var targetRotation = Quaternion.Euler(rotationX, rotationY, 0);

                                                        // 회전           카메라와 플레이어 사이의 거리
        transform.position = followTarget.position - targetRotation  * new Vector3(0, 0, distance);
        transform.rotation = targetRotation;

    }
}

카메라 회전각에 제한걸기(인스펙터창에서 바꿀 수 있음)

    [SerializeField] float minVerticakAngle = -45;
    [SerializeField] float maxVerticakAngle = 45;

기본값을 +-45로

 

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

public class SempleCamera : MonoBehaviour
{
    [SerializeField] Transform followTarget;
    [SerializeField] float distance = 5;

    // 카메라 위아래 회전각 제한걸기
    [SerializeField] float minVerticakAngle = -45;
    [SerializeField] float maxVerticakAngle = 45;

    [SerializeField] Vector2 framingOffset;



    float rotationX;
    float rotationY;
    private void Update()
    {

        // 마우스로 카메라 회전
        rotationX += Input.GetAxis("Mouse Y"); // 위 아래
        rotationX = Mathf.Clamp(rotationX, minVerticakAngle, maxVerticakAngle);

        rotationY += Input.GetAxis("Mouse X"); // 좌우


        var targetRotation = Quaternion.Euler(rotationX, rotationY, 0);

        // 플레이어 따라다니기
                            // 3차원      +        Vector2 framingOffset라서 3차원으로 만들기
        var focusPosition = followTarget.position + new Vector3( framingOffset.x, framingOffset.y);

                                                        // 회전           카메라와 플레이어 사이의 거리
        transform.position = focusPosition - targetRotation  * new Vector3(0, 0, distance);
        transform.rotation = targetRotation;

    }
}

[SerializeField] Vector2 framingOffset;

        인스펙터창에서 각도 제한 바꾸기 (y값만 1로 바꿔보기)

 

 

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

public class SempleCamera : MonoBehaviour
{
    [SerializeField] Transform followTarget;

    [SerializeField] float rotationSpeed = 2f;
    [SerializeField] float distance = 5;

    // 카메라 위아래 회전각 제한걸기
    [SerializeField] float minVerticakAngle = -45;
    [SerializeField] float maxVerticakAngle = 45;

    [SerializeField] Vector2 framingOffset;

    [SerializeField] bool invertX;
    [SerializeField] bool invertY;



    float rotationX;
    float rotationY;

    float invertXVal;
    float invertYVal;

    private void Start()
    {
        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;
    }
    private void Update()
    {
        // invertX가 맞는가? 맞음 : 틀림;
        invertXVal = (invertX) ? -1 : 1;
        invertYVal = (invertY) ? -1 : 1;

        // 마우스로 카메라 회전
        rotationX += Input.GetAxis("Mouse Y") * invertYVal * rotationSpeed; // 위 아래
        rotationX = Mathf.Clamp(rotationX, minVerticakAngle, maxVerticakAngle);

        rotationY += Input.GetAxis("Mouse X") * invertXVal * rotationSpeed; // 좌우


        var targetRotation = Quaternion.Euler(rotationX, rotationY, 0);

        // 플레이어 따라다니기
                            // 3차원      +        Vector2 framingOffset라서 3차원으로 만들기
        var focusPosition = followTarget.position + new Vector3( framingOffset.x, framingOffset.y);

                                                        // 회전           카메라와 플레이어 사이의 거리
        transform.position = focusPosition - targetRotation  * new Vector3(0, 0, distance);
        transform.rotation = targetRotation;

    }
}

 3인칭 게임 화면