본문 바로가기

카테고리 없음

유니티 플레이어 카메라 고정/ 유니티 카메라 고정

728x90

기존 포스팅하였던 카메라 스크립트를 오브젝트에 고정하여 따라올수 있도록하는 스크립트입니다

트렌스폼을 targetTransform으로 하였고, CameraOffset을 사용하였습니다

transform.position = targetTransform.position + CameraOffset; 이렇게 트렌스폼 포지션을 지정하였습니다

 

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

public class CameraController : MonoBehaviour
{
    public float rotateSpeed = 5.0f;
    public float limitAngle = 70.0f;

    private bool isRotate;
    private float mouseX;
    private float mouseY;

    public Transform targetTransform;
    public Vector3 CameraOffset;

    private void Start()
    {

    }

    private void Update()
    {

        //마우스 오른쪽 클릭 시 회전시킴
        if (Input.GetMouseButtonDown(1))
        {
            isRotate = true;
        }
        if (Input.GetMouseButtonUp(1))
        {
            isRotate = false;
        }

        if (isRotate)
        {
            Rotation();
        }
    }

    public void Rotation()
    {
        mouseX += Input.GetAxis("Mouse X") * rotateSpeed; // AxisX = Mouse Y
        mouseY = Mathf.Clamp(mouseY + Input.GetAxis("Mouse Y") * rotateSpeed, -limitAngle, limitAngle);

        //mouseX (가로 움직임) 은 Y축에 영향을 줌
        //mouseY (세로 움직임) 은 X축에 영향을 줌
        transform.rotation = Quaternion.Euler(transform.rotation.x - mouseY, transform.rotation.y + mouseX, 0.0f);

        transform.position = targetTransform.position + CameraOffset;


    }
}

728x90