Hi everyone !
I want to rotate a canvas with my camera when i turn my head.
I don’t want my canvas to rotate when my camera is pointing in its direction.
The canvas should rotate around me when i try to leave it’s direction. So that the canvas follow my eyes.
Here a video of what I did :
As you can see, it works most of the time. Excepted when my rotation goes from 0 to 360.
Here is my code :
[SerializeField] private Transform _Camera, _Canvas, _LeftPoint, _RightPoint, _CenterPoint;
private float _CanvasY, _CameraY, _LeftBuffer, _RightBuffer;
void Start () {
_LeftBuffer = Vector3.Angle( ( _CenterPoint.position - _Camera.position ), ( _LeftPoint.position - _Camera.position ) );
_RightBuffer = Vector3.Angle( ( _CenterPoint.position - _Camera.position ), ( _RightPoint.position - _Camera.position ) );
}
void Update () {
RotateCamera ();
_CameraY = _Camera.rotation.eulerAngles.y;
float min = _CameraY - _RightBuffer;
float max = _CameraY + _LeftBuffer;
_CanvasY = Mathf.Clamp(_CanvasY, min, max);
_Canvas.rotation = Quaternion.Euler(_Canvas.transform.eulerAngles.x, _CanvasY, _Canvas.transform.eulerAngles.z );
}
I can’t figure out was is going on…
_CenterPoint is a point at the center of my canvas, _LeftPoint and _RightPoint are on the left and right of my canvas.
Thanks for your help !