Search Unity

Need help draging Rigidbodys in AR

Discussion in 'AR' started by Dave92FD, May 3, 2019.

  1. Dave92FD

    Dave92FD

    Joined:
    Oct 11, 2018
    Posts:
    1
    Hey,
    I'm making an AR-Escape-Room with ARKit but unfortunately I havent found a good solution for carrying objects around in the virtual room. I'm quite new to coding so I patchworked a script together, but it doesen't seem to be the right thing. (I don't want to use a UI or inventory system to keep the immersion) I've tried different things like a physics gun seen in garry's mod (
    ) but it just doesn't work for me.
    It's very important to move the Rigidbody of the objects to prevent them from doing funny things like shaking with the boxcolliders and have them face the camera/player (seen in the first part of the video). I managed this by using "LookAt()", but this brings the shaking issue back. In line 193 of my code theres a condition so i can controll wich objects should be affected by "LookAt()" using the mass. I dont think thats a ellegant solution, but thats the only thing I came up with... when i took this out of my code, thing just hang around, because I'm using a SpringJoint.

    here's the code I put on the Camera:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5.  
    6. namespace UnityStandardAssets.Utility
    7. {
    8.     public class TouchInput1 : MonoBehaviour
    9.     {
    10.         //Später löschen
    11.         public Transform locTarget;
    12.         public GameObject rotTarget;
    13.  
    14.         public LayerMask touchInputMask;
    15.  
    16.         private List<GameObject> touchList = new List<GameObject>();
    17.         private GameObject[] touchesOld;
    18.         public RaycastHit hit;
    19.  
    20.  
    21.         const float k_Spring = 100.0f;
    22.         const float k_Damper = 0.0f;
    23.         const float k_Drag = 10.0f;
    24.         const float k_AngularDrag = 5.0f;
    25.         const float k_Distance = 0f;
    26.         const bool k_AttachToCenterOfMass = false;
    27.  
    28.         private SpringJoint m_SpringJoint;
    29.  
    30.  
    31.  
    32.  
    33.         // Update is called once per frame
    34.         void Update()
    35.         {
    36.             var mainCamera = FindCamera();
    37.             RaycastHit hit = new RaycastHit();
    38.  
    39. #if UNITY_EDITOR
    40.  
    41.             if (Input.GetMouseButton(0) || Input.GetMouseButtonDown(0) || Input.GetMouseButtonUp(0))
    42.             {
    43.  
    44.                 touchesOld = new GameObject[touchList.Count];
    45.                 touchList.CopyTo(touchesOld);
    46.                 touchList.Clear();
    47.  
    48.                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    49.  
    50.                 if (Physics.Raycast(ray, out hit, touchInputMask))
    51.                 {
    52.  
    53.                     GameObject recipient = hit.transform.gameObject;
    54.                     touchList.Add(recipient);
    55.  
    56.                     if (Input.GetMouseButtonDown(0))
    57.                     {
    58.                         recipient.SendMessage("OnTouchDown", hit.point, SendMessageOptions.DontRequireReceiver);
    59.                     }
    60.                     if (Input.GetMouseButtonUp(0))
    61.                     {
    62.                         recipient.SendMessage("OnTouchUp", hit.point, SendMessageOptions.DontRequireReceiver);
    63.                     }
    64.                     if (Input.GetMouseButton(0))
    65.                     {
    66.                         recipient.SendMessage("OnTouchStay", hit.point, SendMessageOptions.DontRequireReceiver);
    67.                     }
    68.  
    69.                 }
    70.  
    71.  
    72.                 foreach (GameObject g in touchesOld)
    73.                 {
    74.  
    75.                     if (!touchList.Contains(g))
    76.                     {
    77.                         g.SendMessage("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
    78.                     }
    79.  
    80.                 }
    81.  
    82.             }
    83.  
    84.  
    85.  
    86. #endif
    87.  
    88.  
    89.  
    90.             if (Input.touchCount > 0)
    91.             {
    92.  
    93.                 touchesOld = new GameObject[touchList.Count];
    94.                 touchList.CopyTo(touchesOld);
    95.                 touchList.Clear();
    96.  
    97.                 foreach (Touch touch in Input.touches)
    98.                 {
    99.  
    100.                     Ray ray = Camera.main.ScreenPointToRay(touch.position);
    101.  
    102.  
    103.                     if (Physics.Raycast(ray, out hit, touchInputMask))
    104.                     {
    105.  
    106.                         GameObject recipient = hit.transform.gameObject;
    107.                         touchList.Add(recipient);
    108.  
    109.                         if (touch.phase == TouchPhase.Began)
    110.                         {
    111.                             recipient.SendMessage("OnTouchDown", hit.point, SendMessageOptions.DontRequireReceiver);
    112.                         }
    113.                         if (touch.phase == TouchPhase.Ended)
    114.                         {
    115.                             recipient.SendMessage("OnTouchUp", hit.point, SendMessageOptions.DontRequireReceiver);
    116.                         }
    117.                         if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved)
    118.                         {
    119.                             recipient.SendMessage("OnTouchStay", hit.point, SendMessageOptions.DontRequireReceiver);
    120.                         }
    121.                         if (touch.phase == TouchPhase.Canceled)
    122.                         {
    123.                             recipient.SendMessage("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
    124.                         }
    125.                     }
    126.  
    127.                 }
    128.  
    129.                 foreach (GameObject g in touchesOld)
    130.                 {
    131.  
    132.                     if (!touchList.Contains(g))
    133.                     {
    134.                         g.SendMessage("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
    135.                     }
    136.  
    137.                 }
    138.  
    139.             }
    140.  
    141.  
    142.             if (
    143.                 !Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition).origin,
    144.                                  mainCamera.ScreenPointToRay(Input.mousePosition).direction, out hit, 100,
    145.                                  Physics.DefaultRaycastLayers))
    146.             {
    147.                 return;
    148.             }
    149.             // We need to hit a rigidbody that is not kinematic
    150.             if (!hit.rigidbody || hit.rigidbody.isKinematic)
    151.             {
    152.                 return;
    153.             }
    154.  
    155.             if (!m_SpringJoint)
    156.             {
    157.                 var go = new GameObject("Rigidbody dragger");
    158.                 Rigidbody body = go.AddComponent<Rigidbody>();
    159.                 m_SpringJoint = go.AddComponent<SpringJoint>();
    160.                 body.isKinematic = true;
    161.             }
    162.  
    163.             m_SpringJoint.transform.position = hit.point;
    164.             m_SpringJoint.anchor = Vector3.zero;
    165.  
    166.             m_SpringJoint.spring = k_Spring;
    167.             m_SpringJoint.damper = k_Damper;
    168.             m_SpringJoint.maxDistance = k_Distance;
    169.             m_SpringJoint.connectedBody = hit.rigidbody;
    170.  
    171.  
    172.  
    173.             StartCoroutine("DragObject", hit.distance);
    174.  
    175.         }
    176.         private IEnumerator DragObject(float distance)
    177.         {
    178.             var oldDrag = m_SpringJoint.connectedBody.drag;
    179.             var oldAngularDrag = m_SpringJoint.connectedBody.angularDrag;
    180.             m_SpringJoint.connectedBody.drag = k_Drag;
    181.             m_SpringJoint.connectedBody.angularDrag = k_AngularDrag ;
    182.  
    183.  
    184.  
    185.             var mainCamera = FindCamera();
    186.             while (Input.GetMouseButton(0))
    187.             {
    188.                 var ray = mainCamera.ScreenPointToRay(Input.mousePosition);
    189.                 m_SpringJoint.transform.position = ray.GetPoint(distance);
    190.                 yield return null;
    191.  
    192.  
    193.                 //Sorgt für shakes
    194.                 if (m_SpringJoint.connectedBody.mass < 1)
    195.                 {
    196.                     m_SpringJoint.connectedBody.transform.LookAt(locTarget);
    197.                     m_SpringJoint.connectedBody.transform.rotation = rotTarget.transform.rotation;
    198.                 }
    199.                
    200.  
    201.              
    202.             }
    203.             if (m_SpringJoint.connectedBody)
    204.             {
    205.                 m_SpringJoint.connectedBody.drag = oldDrag;
    206.                 m_SpringJoint.connectedBody.angularDrag = oldAngularDrag;
    207.                 m_SpringJoint.connectedBody = null;
    208.  
    209.             }
    210.         }
    211.  
    212.  
    213.         private Camera FindCamera()
    214.         {
    215.             if (GetComponent<Camera>())
    216.             {
    217.                 return GetComponent<Camera>();
    218.             }
    219.  
    220.             return Camera.main;
    221.         }
    222.  
    223.  
    224.      
    225.     }
    226. }
    I've made a video to make the problem clearer (second part without clipping issue, because "LookAt()" is commented out.)



    long story short, the thing I want is: as soon as the player comes close to a carryable object, he should be able to pick it up on touch, drag it by touch or movement of the smartphone and it should face to the player.

    maybe someone comes up with a good solution :)
    Greetings Dave