Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Resolved Drag on one local axis Quest 2

Discussion in 'VR' started by dre38w, Apr 25, 2022.

  1. dre38w

    dre38w

    Joined:
    Nov 18, 2009
    Posts:
    137
    Hello.

    I am trying to drag an object and have it move on only one of its own axes. However, when the objects are rotated it only moves on the world axis. I have tried to use inverdirection, transform direction, point and rigid body constraints and nothing seems to work. Right now I log the initial position in a variable and then apply that position to the axes I want locked and set the desired axis to pull be its own axis value. Does anyone have this problem and a possible solution?

    thank you.
     
  2. dre38w

    dre38w

    Joined:
    Nov 18, 2009
    Posts:
    137
    Got it sorted. Here's the code in case anyone else has this issue:
    Code (CSharp):
    1. //find where objA is currently and where it was originally
    2. offsetFromStart = objA.position - objA.lastPos;
    3.             //project the direction onto the desired vector.
    4.             projectedVector = Vector3.Project(offsetFromStart, orientation.transform.forward);
    5.             //move along that vector
    6.             desiredPos = Vector3.Lerp(thisObjLastPos, thisObjLastPos + projectedVector, precision);
    7.             transform.position = desiredPos;
    ObjA being the object I am dragging. Logging where it is when I initially grab it "objA.lastPos". thisObjLastPos being where the object that I want to actually move was before I grabbed objA. Hope this helps if anyone has this issue!