96 lines
3.0 KiB
C#
96 lines
3.0 KiB
C#
|
|
|||
|
using UnityEngine;
|
|||
|
using UnityEngine.UI;
|
|||
|
|
|||
|
|
|||
|
|
|||
|
public class Display : MonoBehaviour
|
|||
|
{
|
|||
|
[SerializeField] private Transform _displayAnchor;
|
|||
|
[SerializeField] private Camera _camera;
|
|||
|
[SerializeField] private RectTransform _display;
|
|||
|
[SerializeField] private Canvas _canvas;
|
|||
|
private Vector2 _textureDimensions;
|
|||
|
|
|||
|
[SerializeField] private float _rotationSensitivity = 15;
|
|||
|
[SerializeField] private float _minRotationX = -360;
|
|||
|
[SerializeField] private float _maxRotationX= 360;
|
|||
|
[SerializeField] private float _minRotationY = -30;
|
|||
|
[SerializeField] private float _maxRotationY = 30;
|
|||
|
private float _rotationX;
|
|||
|
private float _rotationY;
|
|||
|
private Quaternion _originalRotation;
|
|||
|
|
|||
|
public Transform marker; //Debug
|
|||
|
|
|||
|
// Start is called before the first frame update
|
|||
|
void Start()
|
|||
|
{
|
|||
|
_textureDimensions = new Vector2(_display.GetComponent<RawImage>().texture.width,
|
|||
|
_display.GetComponent<RawImage>().texture.height);
|
|||
|
_originalRotation = _displayAnchor.GetChild(0).localRotation;
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
// Update is called once per frame
|
|||
|
void Update()
|
|||
|
{
|
|||
|
|
|||
|
Vector3 scaledSizeDelta = _display.sizeDelta * _canvas.scaleFactor;
|
|||
|
Vector3 bottomLeftCorner = new Vector3(_display.position.x - (scaledSizeDelta.x / 2),
|
|||
|
_display.position.y - (scaledSizeDelta.y / 2));
|
|||
|
|
|||
|
CastRay(_camera, bottomLeftCorner, scaledSizeDelta, _textureDimensions);
|
|||
|
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
private RaycastHit CastRay(Camera camera, Vector3 bottomLeftCorner, Vector3 sizeDelta, Vector2 textureDimensions)
|
|||
|
{
|
|||
|
|
|||
|
Vector3 relativeMousePosition = Input.mousePosition - bottomLeftCorner;
|
|||
|
relativeMousePosition.x = (relativeMousePosition.x / sizeDelta.x) * textureDimensions.x;
|
|||
|
relativeMousePosition.y = (relativeMousePosition.y / sizeDelta.y) * textureDimensions.y;
|
|||
|
|
|||
|
RaycastHit hit;
|
|||
|
|
|||
|
Ray ray = camera.ScreenPointToRay(relativeMousePosition);
|
|||
|
if (Physics.Raycast(ray, out hit, Mathf.Infinity)) {
|
|||
|
Debug.DrawLine(camera.transform.position, hit.point, Color.blue);
|
|||
|
marker.position = hit.point;
|
|||
|
}
|
|||
|
|
|||
|
return hit;
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
private void RotatePivot(Transform pivot)
|
|||
|
{
|
|||
|
_rotationX += Input.GetAxis("Mouse X") * _rotationSensitivity;
|
|||
|
_rotationY += Input.GetAxis("Mouse Y") * _rotationSensitivity;
|
|||
|
_rotationX = ClampAngle(_rotationX, _minRotationX, _maxRotationX);
|
|||
|
_rotationY = ClampAngle(_rotationY, _minRotationY, _maxRotationY);
|
|||
|
|
|||
|
Quaternion xQuaternion = Quaternion.AngleAxis(_rotationX, Vector2.up);
|
|||
|
Quaternion yQuaternion = Quaternion.AngleAxis(_rotationY, Vector2.left);
|
|||
|
|
|||
|
pivot.localRotation = _originalRotation * xQuaternion * yQuaternion;
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
private float ClampAngle(float angle, float min, float max)
|
|||
|
{
|
|||
|
if (angle > 360)
|
|||
|
angle -= 360;
|
|||
|
|
|||
|
if (angle < -360)
|
|||
|
angle += 360;
|
|||
|
|
|||
|
|
|||
|
return Mathf.Clamp(angle, min, max);
|
|||
|
}
|
|||
|
}
|