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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Grab Rotation Fix

Discussion in 'AR/VR (XR) Discussion' started by AnahidE5, May 2, 2022.

  1. AnahidE5

    AnahidE5

    Joined:
    Jan 10, 2022
    Posts:
    40
    So I understand that XR Interaction Toolkit is coming up with a solution to this. But before that happens, I need to fix this problem for my project.
    When I pick up an object using a ray interactor (or direct interactor), the rotation of the object always resets to match the "hand" or controller. I need the object to keep its rotation. I'm trying to add a script to do this but I can't seem to get it right. I tried to get the rotation this way:
    Code (CSharp):
    1.     void Start()
    2.     {
    3.         interactor = grabee.transform.GetComponent<XRGrabInteractable>();
    4.     }
    5.     void Update()
    6.     {
    7.         interactor.selectEntered.AddListener(GetRotation);
    8.     }
    9.     void FixedUpdate()
    10.     {
    11.         if(interactor.isSelected)
    12.             RotateWith();
    13.  
    14.         if(interactor.isSelected)
    15.             MoveWith();
    16.     }
    17.  
    18.     public void GetRotation(SelectEnterEventArgs args)
    19.     {
    20.         flag = true;
    21.         originalRotation = grabee.transform.localRotation;
    22.     }
    23.  
    24.     public void MoveWith()
    25.     {
    26.         grabee.transform.position = grabber.transform.position;
    27.     }
    28.  
    29.     public void RotateWith()
    30.     {
    31.         if(flag)
    32.         {
    33.             grabee.transform.localRotation = grabber.transform.localRotation * originalRotation;
    34.             flag = false;
    35.         }
    36.         else
    37.         {
    38.             grabee.transform.localRotation = grabber.transform.localRotation;
    39.         }
    40.     }
    But I think I need to get the rotation right before selectEntered. But I don't know how or if that's really the way. Any help would be much appreciated.
     
  2. rickitz5

    rickitz5

    Joined:
    Aug 8, 2021
    Posts:
    31
    I think this is where things could be wrong.

    Multiplication does not seem right, I would have used the object rotation, and then add the hand's rotation like -

    Code (CSharp):
    1. grabee.transform.localRotation =  originalRotation + grabber.transform.localRotation;
    I mean it's totally untested but you could give it a shot.
     
  3. AnahidE5

    AnahidE5

    Joined:
    Jan 10, 2022
    Posts:
    40
    That was my first instinct too but localRotations are of the type Quaternion which you can't use + for. I will try converting them to Euler angle and giving a try though. Thanks.
     
  4. mithrndir

    mithrndir

    Joined:
    Feb 18, 2022
    Posts:
    2
    Hi. Is there any updated code here, including declarations. I'm a unity newbie and am trying to do the same thing with similar issues. Thanks in advance for the help.
     
  5. JulesPeace

    JulesPeace

    Joined:
    Jun 18, 2020
    Posts:
    3
    Same for me. I am working with VRTK 4 grabbers, but the problem should be of the same kind.