카메라 이동
캐릭터의 움직임에 따라 카메라가 캐릭터와 비슷하게 움직이게 해주겠다.
카메라에 붙일 스크립트를 생성한다.
대상 target 을 설정하고 카메라가 이동할 속도, 대상의 현재 위치를 받아온다.
타켓이 존재할 경우 타겟의 움직임에 따라 카메라가 움직인다.
매 프레임 이동할거니까 update에 작성한다.
카메라가 캐릭터랑 동일 z 값에 있으면 둘이 겹치게 되는 문제가 발생하므로 -10 포지션을 유지할 수 있게 해준다.
그리고 카메라랑 캐릭터 위치 사이에서 LERP를 이용해 자연스럽게 카메라가 캐릭터를 따라갈 수 있게 한다.
using UnityEngine;
public class CamareManeger : MonoBehaviour
{
public GameObject target;
public float movespeed;
private Vector3 targetPosition;//대상 현재 위치
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
if (target != null)
{
targetPosition.Set(target.transform.position.x, target.transform.position.y,this.transform.position.z);
this.transform.position = Vector3.Lerp(this.transform.position,targetPosition,movespeed*Time.deltaTime);
}
}
}
이렇게 하고 카메라에 스크립트를 붙힌 뒤 target을 설정해주면 카메라가 잘 따라가는 것을 볼 수 있다.