36 lines
1.1 KiB
C#
36 lines
1.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using BlueWaterProject;
|
|
using UnityEngine;
|
|
|
|
public class LookAtCameraConditionally : MonoBehaviour
|
|
{
|
|
public Transform targetCamera; // 대상 카메라
|
|
public float activeDistance = 20f; // 최대 거리
|
|
|
|
private void Start()
|
|
{
|
|
if (targetCamera == null)
|
|
{
|
|
targetCamera = CameraManager.Inst.MainCam.transform;
|
|
}
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
if (GameManager.Inst.IsOnFollowCamera == false) return;
|
|
|
|
var distanceToCamera = Vector3.Distance(transform.position, targetCamera.position);
|
|
|
|
if (distanceToCamera <= activeDistance)
|
|
{
|
|
var directionToCamera = targetCamera.position - transform.position;
|
|
var lookRotation = Quaternion.LookRotation(directionToCamera);
|
|
|
|
// X축만 카메라를 팔로우
|
|
var currentRotation = transform.eulerAngles;
|
|
transform.eulerAngles = new Vector3(-lookRotation.eulerAngles.x, currentRotation.y, currentRotation.z);
|
|
}
|
|
}
|
|
}
|