Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Object Moving where I touch

Discussion in 'XR Interaction Toolkit and Input' started by afurioso, Nov 20, 2022.

  1. afurioso

    afurioso

    Joined:
    Jan 12, 2022
    Posts:
    88
    Hi everyone,

    I'm using AR foundation and made a script to place the object where I hit. I want to avoid hitting a point in the screen and moving the object there. What I want to achieve is to move the object like a drag and drop.

    What should I change in my script?

    Code (CSharp):
    1.     /*----------------------------------------------------------------
    2.      * PLANE DETECTION
    3.      * ---------------------------------------------------------------*/
    4.     void PlaneSceneActive()
    5.     {
    6.         if (!TryGetTouchPosition(out Vector2 touchPosition))
    7.             return;
    8.  
    9.         if (_aRRaycastManager.Raycast(touchPosition, hits, TrackableType.PlaneWithinPolygon) && !IsTouchOverUI())
    10.         {
    11.             // Raycast hits are sorted by distance, so the first one
    12.             // will be the closest hit.
    13.             var hitPose = hits[0].pose;
    14.  
    15.             if (spawnObjectActive == false && Input.GetMouseButtonDown(0))
    16.             {
    17.                 spawnedObject.transform.position = hitPose.position;
    18.                 spawnedObject.transform.rotation = hitPose.rotation;
    19.                 spawnedObject.SetActive(true);
    20.                 spawnObjectActive = true;
    21.                 placementIndicator.SetActive(false);
    22.             }
    23.  
    24.             GetPositionAndRotation();
    25.         }
    26.     }