90 lines
3.0 KiB
C#
90 lines
3.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.XR.ARFoundation;
|
|
using UnityEngine.XR.ARSubsystems;
|
|
|
|
namespace Assets.Scripts.Furniture
|
|
{
|
|
public class FurnitureDragger : MonoBehaviour
|
|
{
|
|
private Camera _mainCamera;
|
|
private ARRaycastManager _arRaycastManager;
|
|
|
|
private bool _isDragging;
|
|
private bool _isRotating;
|
|
private Vector2 _initialTouchPosition;
|
|
private float _initialRotation;
|
|
|
|
private void Start()
|
|
{
|
|
_mainCamera = Camera.main;
|
|
_arRaycastManager = FindFirstObjectByType<ARRaycastManager>();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (Input.touchCount == 1)
|
|
{
|
|
HandleDrag();
|
|
}
|
|
else if (Input.touchCount == 2)
|
|
{
|
|
HandleRotate();
|
|
}
|
|
}
|
|
|
|
private void HandleRotate()
|
|
{
|
|
Touch touch = Input.GetTouch(0);
|
|
|
|
if (touch.phase == TouchPhase.Began)
|
|
{
|
|
Ray ray = _mainCamera.ScreenPointToRay(touch.position);
|
|
if (Physics.Raycast(ray, out RaycastHit hit) && hit.transform == transform)
|
|
{
|
|
_isDragging = true;
|
|
}
|
|
}
|
|
else if (touch.phase == TouchPhase.Moved && _isDragging)
|
|
{
|
|
List<ARRaycastHit> hits = new();
|
|
if (_arRaycastManager.Raycast(touch.position, hits, TrackableType.PlaneWithinPolygon))
|
|
{
|
|
Pose hitPose = hits[0].pose;
|
|
transform.position = hitPose.position;
|
|
}
|
|
}
|
|
else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled) { }
|
|
{
|
|
_isDragging = false;
|
|
}
|
|
}
|
|
|
|
private void HandleDrag()
|
|
{
|
|
Touch touch1 = Input.GetTouch(0);
|
|
Touch touch2 = Input.GetTouch(1);
|
|
|
|
if (touch1.phase == TouchPhase.Began || touch2.phase == TouchPhase.Began)
|
|
{
|
|
// Registrar el ángulo inicial para la rotación
|
|
Vector2 initialVector = touch2.position - touch1.position;
|
|
_initialRotation = Mathf.Atan2(initialVector.y, initialVector.x) * Mathf.Rad2Deg;
|
|
}
|
|
else if (touch1.phase == TouchPhase.Moved || touch2.phase == TouchPhase.Moved)
|
|
{
|
|
// Calcular el ángulo actual
|
|
Vector2 currentVector = touch2.position - touch1.position;
|
|
float currentRotation = Mathf.Atan2(currentVector.y, currentVector.x) * Mathf.Rad2Deg;
|
|
|
|
// Calcular la diferencia de ángulo y aplicar la rotación
|
|
float deltaRotation = currentRotation - _initialRotation;
|
|
transform.Rotate(Vector3.up, deltaRotation, Space.World);
|
|
|
|
// Actualizar el ángulo inicial
|
|
_initialRotation = currentRotation;
|
|
}
|
|
}
|
|
}
|
|
} |