I’m working with Niantic’s ARDK, and the company asked me to place three objects in the real world so that users can drag and drop them anywhere. I have successfully managed to place the objects, hold them, and drag them correctly when the camera is positioned behind the objects. However, there is a problem when the user positions their camera phone in front of the objects or on the left side, as the objects move in the opposite direction compared to the finger.
This is my code:
private void HeldObject(Touch touch) { Vector2 _touchPosition = touch.position; Ray ray = Camera.main.ScreenPointToRay(touch.position);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.tag == "apple")
{
dragObject = hit.transform;
distanceToCamera = dragObject.position.z - Camera.main.transform.position.z;
relativePositionTouch = new Vector3(_touchPosition.x,
_touchPosition.y, distanceToCamera);
relativePositionTouch = Camera.main.ScreenToWorldPoint(relativePositionTouch);
cameraToObjectOffset = dragObject.position - relativePositionTouch;
isHeld = true;
Debug.Log($"Esta es la fase del touch: {touch.phase}");
}
} }
private void MovedObjet(Touch touch) {
Vector2 _touchPosition = touch.position;
relativePositionTouch = new Vector3(_touchPosition.x,
_touchPosition.y, distanceToCamera);
relativePositionTouch = Camera.main.ScreenToWorldPoint(relativePositionTouch);
dragObject.position = relativePositionTouch + cameraToObjectOffset; }