Unity/소스코드

카메라 Player 따라다니기 다른버젼

세에레_freewing 2023. 3. 15. 16:30
반응형
public class MainCameraController : MonoBehaviour
{
    [SerializeField] Transform player;
    private void LateUpdate()
    {
        Vector3 targetPos = new Vector3(player.position.x, player.position.y, this.transform.position.z);
        transform.position = targetPos;
    }
}

카메라가 따라갈 위치 변수를 만들어주고, 카메라 위치를 위치 변수로 설정하면 된다.

중요한 건 LateUpdate메서드를 사용해야 한다는 것이다. 

플레이어가 움직이면 위치가 변경되는데 카메라 위치도 같은 프레임에 변경되게 하면 카메라 위치가 버벅된다.

플레이어 위치가 변경된 후 카메라 위치가 변경되게 하자.

Update보다 늦게 동작되는 LateUpdate를 사용해야 한다.

 

using UnityEngine;

public class MainCameraController : MonoBehaviour
{
    [SerializeField] Transform player;
    [SerializeField] float smoothing = 0.2f;
    private void FixedUpdate()
    {
        Vector3 targetPos = new Vector3(player.position.x, player.position.y, this.transform.position.z);
        transform.position = Vector3.Lerp(transform.position, targetPos, smoothing);
    }
    /*private void LateUpdate()
    {
        Vector3 targetPos = new Vector3(player.position.x, player.position.y, this.transform.position.z);
        transform.position = targetPos;
    }*/
}

부드럽게 변경

 

Vector3.Lerp를 사용하는데 여기서는 FixedUpdate를 사용한다.

(캐릭터는 FixedUpdate로 움직이게 해 놨기에 FixedUpdate사용, 만약 Update를 사용했다면 Update로 다르면 버벅된다)

Lerp자체가 설정한 float에 따라 부드럽게 변경되기에 LateUpdate를 사용하면 버벅된다.

 

using UnityEngine;

public class MainCameraController : MonoBehaviour
{
    [SerializeField] Transform player;
    [SerializeField] float smoothing = 0.2f;
    [SerializeField] Vector2 minCameraBoundary;
    [SerializeField] Vector2 maxCameraBoundary;
    private void FixedUpdate()
    {
        Vector3 targetPos = new Vector3(player.position.x, player.position.y, this.transform.position.z);

        targetPos.x = Mathf.Clamp(targetPos.x, minCameraBoundary.x, maxCameraBoundary.x);
        targetPos.y = Mathf.Clamp(targetPos.y, minCameraBoundary.y, maxCameraBoundary.y);

        transform.position = Vector3.Lerp(transform.position, targetPos, smoothing);
    }

간단하게 카메라 경계 설정

 

카메라 위치는 타겟 위치를 따라가므로 타깃 위치에 제한을 준다.

Mathf.Clamp를 사용해 제한을 주자.

 

using UnityEngine;

// Mathf.Clamp 예제. 
// 
// 사인파를 사용하여 x축을 따라 큐브를 애니메이션합니다. 
// x축의 최소 및 최대 위치를 
// 변경합니다. 큐브는
// 최소값과 최대값 안에 표시됩니다 . 

public class ExampleScript : MonoBehaviour
{
    private float xMin = -0.5f, xMax = 0.5f;
    private float timeValue = 0.0f;

    void Update()
    {
        // sin 위치를 계산합니다. 
        float xValue = Mathf.Sin(timeValue * 5.0f);

         // 이제 클램프 값을 계산합니다. 
        float xPos = Mathf.Clamp(xValue, xMin, xMax);

        // 큐브의 위치를 ​​업데이트합니다 . 
        transform.position = new Vector3(xPos, 0.0f, 0.0f);

         // 애니메이션 시간을 늘립니다. 
        timeValue = timeValue + Time.deltaTime;

        // 애니메이션 시간이 계획된 시간보다 길면 재설정합니다.
        if (xValue > Mathf.PI * 2.0f)
        {
            timeValue = 0.0f;
        }
    }

    void OnGUI()
    {
        // 최소값과 최대값을 변경하게 함 
        xMin = GUI.HorizontalSlider(new Rect(25, 25, 100, 30), xMin, -1.0f, +1.0f);
        xMax = GUI.HorizontalSlider(new Rect(25, 60, 100, 30), xMax, -1.0f, +1.0f);

         // xMin은 xMax보다 작거나 같게 유지됩니다. 
        if (xMin > xMax)
        {
            xMin = xMax;
        }

        // 더 나은 크기 레이블로 xMin 및 xMax 값을 표시합니다
        GUIStyle fontSize = new GUIStyle(GUI.skin.GetStyle("label"));
        fontSize.fontSize = 24;

        GUI.Label(new Rect(135, 10, 150, 30), "xMin: " + xMin.ToString("f2"), fontSize);
        GUI.Label(new Rect(135, 45, 150, 30), "xMax: " + xMax.ToString("f2"), fontSize);
    }
}

 

 

반응형