유니티 에디터에서 실행하지 않은 상태에서도 사용가능한 카메라 룩엣 스크립트

 

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using UnityEngine;
 
[ExecuteInEditMode]
public class SmoothLookAtEditor : MonoBehaviour
{
    public Transform target;
    public float smoothSpeed = 5f; // 부드러운 이동을 위한 속도 조절
 
    private Vector3 initialPosition; // 초기 카메라 위치
    private Quaternion initialRotation; // 초기 카메라 회전
 
    private void Start()
    {
        initialPosition = transform.position;
        initialRotation = transform.rotation;
    }
 
    private void Update()
    {
        if (target != null)
        {
            // 카메라의 위치를 target의 위치로 설정하고, 카메라가 target을 향하도록 회전시킵니다.
            //transform.LookAt(target);
 
            // 대상 방향 계산
            //Vector3 targetDirection = target.position - transform.position;
            //Quaternion targetRotation = Quaternion.LookRotation(targetDirection);
 
            // 대상의 위치와 카메라의 y 좌표를 동일하게 설정
            Vector3 targetPosition = new Vector3(target.position.x, transform.position.y, target.position.z);
            Vector3 targetDirection = targetPosition - transform.position;
            Quaternion targetRotation = Quaternion.LookRotation(targetDirection);
 
            // 부드러운 회전 및 이동
            transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, smoothSpeed * Time.deltaTime);
            transform.position = Vector3.Lerp(transform.position, initialPosition, smoothSpeed * Time.deltaTime);
        }
        else
        {
            // 타겟이 없는 경우 초기 위치와 회전으로 복원
            transform.position = Vector3.Lerp(transform.position, initialPosition, smoothSpeed * Time.deltaTime);
            transform.rotation = Quaternion.Slerp(transform.rotation, initialRotation, smoothSpeed * Time.deltaTime);
        }
    }
}
 
cs

 

출처: ChatGPT 3.5

+ Recent posts