Search Unity

Question Move the object only when is touched

Discussion in 'Scripting' started by afurioso, Nov 23, 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.     }
    27.  
    28.    void GetPositionAndRotation()
    29.     {
    30.         var hitPose = hits[0].pose;
    31.         if (Input.touchCount == 1 && spawnObjectActive == true)
    32.         {
    33.             spawnedObject.transform.position = hitPose.position;
    34.         }
    35.      }
    36.  
    37.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    This would be a great place to begin your investigations:

    Screen Shot 2022-11-23 at 10.32.21 AM.png

    If you run into difficulties, here is how to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of this forum is to assist people who are ready to learn by doing, and who are unafraid to get their hands dirty learning how to code, particularly in the context of Unity3D.

    This assumes you have at least written and studied some code and have run into some kind of issue.

    If you haven't even started yet, go check out some Youtube videos for whatever game design you have in mind. There are already many examples of the individual parts and concepts involved, as there is nothing truly new under the sun.

    If you just want someone to do it for you, you need go to one of these places:

    https://forum.unity.com/forums/commercial-job-offering.49/

    https://forum.unity.com/forums/non-commercial-collaboration.17/

    https://livehelp.unity.com/?keywords=&page=1&searchTypes=lessons