sea-battle-vr/Assets/Scripts/Common/VR/YawOnlyTracker.cs

54 lines
1.6 KiB
C#

using UnityEngine;
public class YawOnlyTracker : MonoBehaviour
{
[SerializeField] private Transform cameraTransform;
[SerializeField] private float sensitivity = 1;
[SerializeField] private float offset = 0;
private void Start()
{
if (cameraTransform == null)
{
Debug.LogError("Камера не указана", this);
return;
}
}
public void SetConfig(PeriscopeAnglesConfig config)
{
sensitivity = config.sensitivity;
offset = config.offset;
}
void LateUpdate()
{
//Quaternion currentRotation = cameraTransform.localRotation;
//Vector3 eulerAngles = currentRotation.eulerAngles;
//Quaternion yawOnly = Quaternion.Euler(0f, eulerAngles.y /** Config.sensitivity*/, 0f);
//cameraTransform.localRotation = yawOnly;
Quaternion currentRotation = cameraTransform.localRotation;
Vector3 eulerAngles = currentRotation.eulerAngles;
// Нормализуем угол в диапазон [-180, 180]
float normalizedYaw = NormalizeAngle(eulerAngles.y + offset);
// Применяем sensitivity
float adjustedYaw = normalizedYaw * sensitivity;
// Применяем поворот (Unity сама нормализует при установке)
Quaternion yawOnly = Quaternion.Euler(0f, adjustedYaw, 0f);
cameraTransform.localRotation = yawOnly;
}
public static float NormalizeAngle(float angle)
{
angle %= 360f;
if (angle > 180f) angle -= 360f;
if (angle < -180f) angle += 360f;
return angle;
}
}