89 lines
2.7 KiB
C#
89 lines
2.7 KiB
C#
using UnityEngine;
|
|
|
|
public class RotateAndMoveObjectAR : MonoBehaviour
|
|
{
|
|
public GameObject furniture;
|
|
public Camera arCamera;
|
|
public float rotationSpeed = 100f;
|
|
public float moveSpeed = 0.01f;
|
|
|
|
private bool isRotating;
|
|
private bool isMoving;
|
|
private Vector2 lastTouchPosition;
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
HandleRotation();
|
|
HandleMovement();
|
|
}
|
|
|
|
void HandleRotation()
|
|
{
|
|
// Comprobar si hay un segundo dedo en la pantalla (para rotación)
|
|
if (Input.touchCount == 1)
|
|
{
|
|
Touch touch = Input.GetTouch(0);
|
|
|
|
if (touch.phase == TouchPhase.Began)
|
|
{
|
|
lastTouchPosition = touch.position;
|
|
isRotating = true;
|
|
}
|
|
else if (touch.phase == TouchPhase.Moved && isRotating)
|
|
{
|
|
Vector2 touchDelta = touch.position - lastTouchPosition;
|
|
float rotationAmount = touchDelta.x * rotationSpeed * Time.deltaTime;
|
|
|
|
// Rotar el objeto sobre el eje Y
|
|
furniture.transform.Rotate(0, -rotationAmount, 0);
|
|
|
|
lastTouchPosition = touch.position;
|
|
}
|
|
else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
|
|
{
|
|
isRotating = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
void HandleMovement()
|
|
{
|
|
// Comprobar si hay un toque en la pantalla para mover el objeto
|
|
if (Input.touchCount == 1)
|
|
{
|
|
Touch touch = Input.GetTouch(0);
|
|
|
|
if (touch.phase == TouchPhase.Began)
|
|
{
|
|
Ray ray = arCamera.ScreenPointToRay(touch.position);
|
|
if (Physics.Raycast(ray, out RaycastHit hit))
|
|
{
|
|
if (hit.collider.gameObject == furniture)
|
|
{
|
|
isMoving = true;
|
|
}
|
|
}
|
|
}
|
|
else if (touch.phase == TouchPhase.Moved && isMoving)
|
|
{
|
|
// Calcular la nueva posición en el mundo
|
|
Ray ray = arCamera.ScreenPointToRay(touch.position);
|
|
if (Physics.Raycast(ray, out RaycastHit hit))
|
|
{
|
|
// Mueve el mueble al punto donde toca el rayo
|
|
furniture.transform.position = Vector3.Lerp(
|
|
furniture.transform.position,
|
|
hit.point,
|
|
moveSpeed
|
|
);
|
|
}
|
|
}
|
|
else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
|
|
{
|
|
isMoving = false;
|
|
}
|
|
}
|
|
}
|
|
}
|