티스토리 뷰

카테고리 없음

본캠프_TIL_20일차

티-히히 2024. 10. 11. 21:30

오늘 한 일

 

개인과제 진행중입니다.

 

1. 캐릭터 마우스 시선처리

2. 타일맵제작 콜리더 적용

3. 카메라 따라가기

4. 이름 입력시스템

 

 

3. 카메라 따라가기

 

https://hoil2.tistory.com/31

 

[유니티 강좌] 2D RPG 게임 만들기 - 10 / 따라오는 카메라, 영역 설정

이번 포스팅은 자세히 이해하지 않아도 괜찮으니 바쁘신 분들은 스크립트만 가져가셔도 괜찮습니다. public class CameraFollow : MonoBehaviour { public Transform target; public float smoothSpeed = 3; public Vector2 offset;

hoil2.tistory.com

이 블로그에서 도움을 많이 얻었습니다

다만 public float limitMinX, limitMaxX, limitMinY, limitMaxY 를 직접 입력해줘야한다는게 번거로웠습니다

 

타일맵을 기준으로 값을 가져올 수 있도록 코드를 수정했습니다.

 

public class CameraFollow : MonoBehaviour
{
    public Transform target;

    public float smoothSpeed = 5;
    public Vector2 offset;

    public float limitMinX, limitMaxX, limitMinY, limitMaxY;
    float cameraHalfWidth, cameraHalfHeight;

    public Tilemap tilemap;
    private void Start()
    {
        cameraHalfWidth = Camera.main.aspect * Camera.main.orthographicSize;
        cameraHalfHeight = Camera.main.orthographicSize;

        if (tilemap != null)
        {
            SetCameraLimitsFromTilemap();
        }
    }

    private void LateUpdate()
    {
        Vector3 desiredPosition = new Vector3(
            Mathf.Clamp(target.position.x + offset.x, limitMinX + cameraHalfWidth, limitMaxX - cameraHalfWidth),   // X
            Mathf.Clamp(target.position.y + offset.y, limitMinY + cameraHalfHeight, limitMaxY - cameraHalfHeight), // Y
            -10);                                                                                                  // Z
        transform.position = Vector3.Lerp(transform.position, desiredPosition, Time.deltaTime * smoothSpeed);
    }

    private void SetCameraLimitsFromTilemap()
    {
        BoundsInt bounds = tilemap.cellBounds;

        // Convert the bounds to world coordinates
        Vector3 min = tilemap.CellToWorld(bounds.min);
        Vector3 max = tilemap.CellToWorld(bounds.max);

        // Set the camera limits
        limitMinX = min.x;
        limitMaxX = max.x;
        limitMinY = min.y;
        limitMaxY = max.y;
    }
}

 

 

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/03   »
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 29
30 31
글 보관함