52 lines
1.6 KiB
C#
52 lines
1.6 KiB
C#
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
public class LookAtToggleUI : MonoBehaviour
|
||
|
|
{
|
||
|
|
[Header("References")]
|
||
|
|
[SerializeField] private Transform source; // рука / контроллер / камера
|
||
|
|
[SerializeField] private Transform target; // лицо / голова / объект
|
||
|
|
[SerializeField] private GameObject uiObject;
|
||
|
|
|
||
|
|
[Header("Settings")]
|
||
|
|
[SerializeField, Range(0f, 1f)] private float threshold = 0.9f;
|
||
|
|
[SerializeField] private bool useDebug = true;
|
||
|
|
[SerializeField] private float debugInterval = 0.1f;
|
||
|
|
|
||
|
|
private float timer;
|
||
|
|
|
||
|
|
private void Start()
|
||
|
|
{
|
||
|
|
uiObject.SetActive(false);
|
||
|
|
}
|
||
|
|
|
||
|
|
private void Update()
|
||
|
|
{
|
||
|
|
if (source == null || target == null) return;
|
||
|
|
|
||
|
|
Vector3 toTarget = (target.position - source.position).normalized;
|
||
|
|
Vector3 forward = source.forward.normalized;
|
||
|
|
|
||
|
|
float dot = Vector3.Dot(forward, toTarget);
|
||
|
|
|
||
|
|
bool isFacing = dot > threshold;
|
||
|
|
uiObject.SetActive(isFacing);
|
||
|
|
|
||
|
|
if (useDebug)
|
||
|
|
{
|
||
|
|
DebugDraw(toTarget, forward, dot);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void DebugDraw(Vector3 toTarget, Vector3 forward, float dot)
|
||
|
|
{
|
||
|
|
timer += Time.deltaTime;
|
||
|
|
if (timer < debugInterval) return;
|
||
|
|
timer = 0f;
|
||
|
|
|
||
|
|
Debug.DrawRay(source.position, forward, Color.blue, debugInterval);
|
||
|
|
Debug.DrawRay(source.position, toTarget, Color.red, debugInterval);
|
||
|
|
Debug.DrawLine(source.position, target.position, Color.green, debugInterval);
|
||
|
|
|
||
|
|
Debug.Log($"Dot: {dot:F2}");
|
||
|
|
}
|
||
|
|
}
|