VR4RoboticArm2/VR4RoboticArm/Assets/Scripts/Distantator.cs
2025-09-13 11:51:52 +03:00

82 lines
2.4 KiB
C#

using Meta.WitAi.Utilities;
using Unity.Mathematics;
using UnityEngine;
public class Distantator : MonoBehaviour
{
[SerializeField] private Transform beamOne;
[SerializeField] private Transform beamTwo;
[SerializeField] private Transform SecondbeamOne;
[SerializeField] private Transform SecondbeamTwo;
[SerializeField,Tooltip("It should be -1 or 1")] private int direction;
[SerializeField] private float distance;
private float beamDistance = 0.214017f;
private float totalBase = 0.436546f;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
float angle = calculateTheAngle();
setTheAngle(angle);
if(SecondbeamOne != null)
{
setTheSecondAngle(angle);
}
}
private void setTheSecondAngle(float angle)
{
if(direction ==-1)
{
SecondbeamOne.transform.localEulerAngles = new Vector3(0, -90f-angle, 0);
SecondbeamTwo.transform.localEulerAngles = new Vector3(0,angle-90f,0);
}
else
{
SecondbeamOne.transform.localEulerAngles = new Vector3(0, 90f + angle, 0);
SecondbeamTwo.transform.localEulerAngles = new Vector3(0, -angle + 90f, 0);
}
}
private void setTheAngle(float angle)
{
if(direction == -1)
{
beamOne.transform.localEulerAngles = new Vector3(0f, angle, 0f);
beamTwo.transform.localEulerAngles = new Vector3(0f, -angle, 0f);
}
else
{
beamOne.transform.localEulerAngles = new Vector3(0f, -angle, 0f);
beamTwo.transform.localEulerAngles = new Vector3(0f, angle, 0f);
}
}
private float calculateTheAngle()
{
beamDistance = beamDistance * 1.6f;
float pertwo = (totalBase * 1.6f / 2) - (distance / 2);
float height = math.sqrt(math.pow(beamDistance, 2) - math.pow(pertwo, 2));
float cosAlfa = (math.pow(beamDistance, 2) + math.pow(pertwo, 2) - math.pow(height, 2)) / (2 * beamDistance * pertwo);
cosAlfa = Mathf.Clamp(cosAlfa, -1f, 1f);
float alfa = math.acos(cosAlfa);
float alfaDegrees = alfa * Mathf.Rad2Deg;
Debug.Log(alfaDegrees);
return alfaDegrees;
}
}