플레이어들의 평균위치 구하기 / 영역 제한 / ZOOM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMove2D_LHS : MonoBehaviour
{
// 플레이어 중심점을 찾기 위한 변수
public Transform target1;
public Transform target2;
// 이동 속도
public float speed;
//카메라 제한 영역
//중심점
public Vector2 center;
//크기
public Vector2 size;
float height;
float width;
void Start()
{
// 시작 할 때 위치 = 타겟 위치로
//transform.position = target.position;
//카메라 x 회전 각도
transform.eulerAngles = new Vector3(6, 0, 0);
//카메라의 월드공간에서 가로/ 세로의 크기를 구해야함
//세로 절반 크기
height = Camera.main.orthographicSize;
//월드 가로 = 월드 세로 * 스크린 가로/ 스크린 세로
width = height * Screen.width / Screen.height;
}
//영역제한 변수값을 시각적으로 보기 위해 함수 생성
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireCube(center, size);
}
// Update is called once per frame
void Update()
{
// 변수에 담으려고 했는데 담기지 않음
//centerTarget.position = (target1.position + target2.position) * 0.5f;
// 중심점을 기준으로 (플레이어들의 평균위치)
// Lerp를 이용한 카메라 이동(target 따라다니게)
transform.position = Vector3.Lerp(transform.position,(target1.position + target2.position) * 0.5f + new Vector3(0,3,0), Time.deltaTime * speed);
// 카메라 영역제한
// Mathf.Clamp(value,min,max)
// value값이 min과 max의 사이면 value를 반환하고
// value값이 min보다 작으면 min을 max보다 크면 max를 반환
float lx = size.x * 0.5f - width;
float clampX = Mathf.Clamp(transform.position.x, -lx + center.x, lx + center.x);
float ly = size.y * 0.5f - height;
float clampY = Mathf.Clamp(transform.position.y, -ly + center.y, lx + center.y);
transform.position = new Vector3(clampX, clampY, -10f);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraZoom2D_LHS : MonoBehaviour
{
// Test용
//public bool ZoomActive;
public Camera Cam;
// Lerp 속도
public float Speed;
// 플레이어들
public GameObject target1;
public GameObject target2;
//타겟의 거리
float distance_target;
// Start is called before the first frame update
void Start()
{
Cam = Camera.main;
}
public void LateUpdate()
{
// 줌 아웃
Cam.orthographicSize = Mathf.Lerp(Cam.orthographicSize, distance_target, Speed);
//if(ZoomActive)
//{
// Cam.orthographicSize = Mathf.Lerp(Cam.orthographicSize, 5, Speed);
//}
//else
//{
// Cam.orthographicSize = Mathf.Lerp(Cam.orthographicSize, 8, Speed);
//}
// 두 타겟 사이의 거리
distance_target = Vector3.Distance(target1.transform.position, target2.transform.position);
if (distance_target < 4)
{
distance_target = 4;
}
else if (distance_target > 5)
{
distance_target = 5;
}
// 두 타겟의 거리가 5가 넘으면 ZoomActive 가 켜지게
//if (Vector3.Distance(target1.transform.position, target2.transform.position) < 5)
//{
// ZoomActive = true;
//}
//else
//{
// ZoomActive = false;
//}
}
void Update()
{
}
}
728x90
'유니티 프로젝트 > Team Project3 - 대난투' 카테고리의 다른 글
MULTIPLE TARGET CAMERA 3D (0) | 2022.09.12 |
---|