Search Unity

Moving Objects While Snapped To The Ground

Discussion in 'AR/VR (XR) Discussion' started by Vladislav-Videnov, Apr 15, 2019.

  1. Vladislav-Videnov

    Vladislav-Videnov

    Joined:
    Nov 19, 2015
    Posts:
    18
    Hello guys.

    I am working on a Virtual Reality project using the HTC Vive. I have to create different ways of interacting with objects.

    Currently i have a "Laser" on the right controller which lets the user interact with objects (Highlight,Select, Manipulate). Manipulating is like moving, rotating and scaling.

    I am stuck on the moving part as i cant achieve the desired behavior.

    Here is how it looks like now : https://imgur.com/0WXOsQe


    Here is the code i am using to place it on the ground position:
    Code (CSharp):
    1.   private void SnappingObject()
    2.          {
    3.              RaycastHit newHit;
    4.              if (Physics.Raycast(transform.position, transform.forward, out newHit, rayDistance, GroundLayerMask)) //to the ground - ignoring object
    5.              {
    6.                  ObjectToEdit.transform.position = newHit.point;
    7.              }
    8.          }

    I would like the object to not SNAP to the new position the ground raycast detects, but keep its position and move based on how much the "Laser" moved.

    I tried multiple ways of fixing it but none of them worked as my trigonometry skills have slowly started to deplete when i am not using them :(. I tried calculating a the delta from the ground position found when user grabbed the object and while moving, then add the delta to the object position, but that resulted in very weird behavior.

    I tried to look anywhere on the internet how this can be solved, but found nothing relative.

    Anybody has any suggestions or a solution ?

    Thank you very much !
     
  2. DebugLogWarning

    DebugLogWarning

    Joined:
    Jun 28, 2018
    Posts:
    14
    Try calculating the delta only when you first start the interaction, then each subsequent frame add the initial delta to the hit.point (don't continuously re-calculate the delta). This should preserve the initial offset relative to the position of the laser.

    You could also try recording the first position of the laser and the first position of the object. Then each frame subtract the current position of the laser from the initial position of the laser and add that delta to the initial position of the game object for a different, but similar behavior.
     
  3. lowbl_dan

    lowbl_dan

    Joined:
    Jan 22, 2019
    Posts:
    34
    You can have a predefined object that is constantly set to to your laser's hit.point, and simply parent the hit.collider ( the box) to the predefined object on controller trigger pressed and unparent it on controller trigger release.
     
    Last edited: Apr 23, 2019
  4. Vladislav-Videnov

    Vladislav-Videnov

    Joined:
    Nov 19, 2015
    Posts:
    18
    I attempted to do something similar to what you proposed and it kind of works.. The distance amount from OnGrabHitPoint and CurrentHitPoint is not always good and it the object starts to offset from the laser (keeps the relative positon on grab though, but when moving .. it starts offsetting a lot) . There is also a problem if i want to place the object on top of another :/
     
  5. Vladislav-Videnov

    Vladislav-Videnov

    Joined:
    Nov 19, 2015
    Posts:
    18
    Nice idea, works similar as what DebugLogWarning proposed as a solution.. Both ways fail when the ground level changes (putting an object on the table for example) .. It creates a weird offset and the moved object kinda is off the laser pointer .. :( ..
    I guess i have to make a fixed point (around the origin) where the object can be grabbed and then the jumping object issue is gone...
     
  6. lowbl_dan

    lowbl_dan

    Joined:
    Jan 22, 2019
    Posts:
    34
    if you want to resolve the offset using the parenting method, you may want to constantly set the position of the referenceobject such that
    Code (csharp):
    1.  
    2.   if(hit.collider)
    3.  {
    4.         laserReferenceObject.transform.position = rayDirection * hit.point.magnitude;
    5.         hit.transform.SetParent(laserReferenceObject);
    6.  }
    7.  
    This is my best guess though.
     
  7. anga7267

    anga7267

    Joined:
    Jul 13, 2020
    Posts:
    1
    Hello @Vladislav-Videnov ! It might be useless to write here now, but I'm really interested in how you did the thing work... I'm struggeling with the interaction to objects and what you did is exactly what I need to achieve: moving objects around with my ray... Can you please tell me more about it and maybe show me your code? Would appreciate it, thx!