티스토리 뷰

카메라가 오브젝트를 따라가게 하는 방법은 간단하다.

    public Transform target; // camera가 따라갈 오브젝트의 transform

    public float smoothSpeed = 0.125f;
    public Vector3 offset;

    private void LateUpdate()
    {
        transform.position = target.position + offset;
    }

 

transform.position = target.position + offset;

의 의미를 정확히 살펴보면.

 

transform.position은 현재 이 스크립트가 부착된 오브젝트의 transform, 따라서 카메라의 위치다.

target.position은 내가 따라갈 오브젝트의 위치 정보다.

그렇다면 offset은?

 

현재 오브젝트가 씬에서 다음과 같이있다

 

만약 + offset을 없에고 다음과 같이 쓰고 게임을 플레이하면 

public Transform target;

    public float smoothSpeed = 0.125f;
    public Vector3 offset;

    private void LateUpdate()
    {
        transform.position = target.position;
    }

게임 화면은 다음과 같이 나온다.

게임 화면

 

씬 화면

카메라가 target.position으로 고정되서 오브젝트에 붙어버렸다.

 

 

+ offset을 붙이면

public Transform target;

    public float smoothSpeed = 0.125f;
    public Vector3 offset = new Vector3(0f, 1f, -6f);

    private void LateUpdate()
    {
        transform.position = target.position + offset;
    }

씬화면
offset vector3 값을 0,1,-6으로 설정

이렇게 카메라위치가 바뀌고

게임 화면

이렇게 정상적으로 오브젝트가 보이고 오브젝트가 이동하면 카메라도 같이 이동한다.

 

즉 target.postition = (0, 0, 0)이고

offset 벡터가 = (0, 1, -6) 이면 둘이 더해져서

(0, 0, 0) + (0, 1, -6) = (0, 1, -6)이 카메라의 위치가 되는것이다. 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2026/02   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
글 보관함