95 lines
2.7 KiB
C#
95 lines
2.7 KiB
C#
using UnityEngine;
|
|
using Unheimlich.Gameplay.Player;
|
|
|
|
public class SecurityCameraController : MonoBehaviour
|
|
{
|
|
[Header("Tracking Settings")]
|
|
[SerializeField] private float _trackingDistance = 10f;
|
|
[SerializeField] private float _rotationSpeed = 2f;
|
|
[SerializeField] private Transform _pivotPoint;
|
|
|
|
[Header("LED Settings")]
|
|
[SerializeField] private MeshRenderer _ledRenderer;
|
|
[SerializeField] private float _blinkSpeed = 2f;
|
|
[SerializeField] private float _minSmoothness = 0f;
|
|
[SerializeField] private float _maxSmoothness = 0.5f;
|
|
|
|
private CharacterController _playerController;
|
|
private Material _ledMaterial;
|
|
private float _blinkTimer;
|
|
private bool _isTracking;
|
|
|
|
void Start()
|
|
{
|
|
if (PlayerController.Instance != null)
|
|
_playerController = PlayerController.Instance.GetComponent<CharacterController>();
|
|
|
|
if (_ledRenderer != null)
|
|
{
|
|
_ledMaterial = _ledRenderer.material;
|
|
}
|
|
|
|
if (_pivotPoint == null)
|
|
_pivotPoint = transform;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (_playerController == null) return;
|
|
|
|
float distanceToPlayer = Vector3.Distance(_pivotPoint.position, _playerController.transform.position);
|
|
_isTracking = distanceToPlayer <= _trackingDistance;
|
|
|
|
if (_isTracking)
|
|
{
|
|
TrackPlayer();
|
|
}
|
|
|
|
UpdateLedBlink();
|
|
}
|
|
|
|
void TrackPlayer()
|
|
{
|
|
Vector3 directionToPlayer = _playerController.transform.position - _pivotPoint.position;
|
|
|
|
if (directionToPlayer.sqrMagnitude > 0.001f)
|
|
{
|
|
Vector3 forward = -directionToPlayer.normalized;
|
|
Vector3 right = Vector3.Cross(Vector3.up, forward).normalized;
|
|
Vector3 up = Vector3.Cross(forward, right);
|
|
|
|
Quaternion targetRotation = Quaternion.LookRotation(up, forward);
|
|
_pivotPoint.rotation = Quaternion.Slerp(_pivotPoint.rotation, targetRotation, Time.deltaTime * _rotationSpeed);
|
|
}
|
|
}
|
|
|
|
void UpdateLedBlink()
|
|
{
|
|
if (_ledMaterial == null) return;
|
|
|
|
_blinkTimer += Time.deltaTime * _blinkSpeed;
|
|
|
|
float smoothness = Mathf.Lerp(_minSmoothness, _maxSmoothness, Mathf.PingPong(_blinkTimer, 1f));
|
|
|
|
_ledMaterial.SetFloat("_Smoothness", smoothness);
|
|
}
|
|
|
|
void OnDestroy()
|
|
{
|
|
if (_ledMaterial != null)
|
|
Destroy(_ledMaterial);
|
|
}
|
|
|
|
void OnDrawGizmosSelected()
|
|
{
|
|
if (_pivotPoint == null)
|
|
_pivotPoint = transform;
|
|
|
|
Gizmos.color = Color.yellow;
|
|
Gizmos.DrawWireSphere(_pivotPoint.position, _trackingDistance);
|
|
|
|
Gizmos.color = Color.red;
|
|
Gizmos.DrawLine(_pivotPoint.position, _pivotPoint.position + _pivotPoint.forward * 2f);
|
|
}
|
|
}
|