Search Unity

Throwing Object Issue Oculus

Discussion in 'AR/VR (XR) Discussion' started by Mossimo, Feb 11, 2018.

  1. Mossimo

    Mossimo

    Joined:
    Feb 25, 2017
    Posts:
    2
    Hi friends,

    So it's been a while since I've used Unity and I'm completely new to working with the Oculus Rift and Touch Controllers. I figured I would start out with some basic grabbing and throwing so I found and used these tutorials from Ben Roberts.

    Everything was working fine until I introduced some code for rotating the player and now this happens after I rotate the player controller and throw an object.

    Basically, when the scene starts I can throw my object around the scene without issue but when I rotate my character and pick up the same object and throw it the object will fly in the forward direction correlating to my original position/rotation and not the forward direction of my current position/rotation.

    Any help would be greatly appreciated.

    Drop/Throw Code:
    Code (CSharp):
    1. void DropObject() {
    2.     grabbing = false;
    3.  
    4.     if (grabbedObject != null) {
    5.         grabbedObject.transform.parent = null;
    6.         grabbedObject.GetComponent<Rigidbody>().isKinematic = false;
    7.         grabbedObject.GetComponent<Rigidbody>().velocity = OVRInput.GetLocalControllerVelocity(touchController) * speed;
    8.         grabbedObject.GetComponent<Rigidbody>().angularVelocity = OVRInput.GetLocalControllerAngularVelocity(touchController);
    9.         grabbedObject.GetComponent<Rigidbody>().maxAngularVelocity = grabbedObject.GetComponent<Rigidbody>().angularVelocity.magnitude;
    10.  
    11.         grabbedObject = null;
    12.     }
    13. }
     
  2. Johnny_Jr

    Johnny_Jr

    Joined:
    Jan 23, 2015
    Posts:
    9
    Your problem is that you are using the local velocity but applying it to the grabbed object that is in world space. You need to translate the velocity vector to world space.
     
  3. Mossimo

    Mossimo

    Joined:
    Feb 25, 2017
    Posts:
    2
    Hey Johnny_Jr, thanks for replying.

    So I understand what you're saying about translating the velocity vector to world space but I am having trouble implementing it. From my research, it looks like I should be using Transform.TransformDirection to convert my OVRInput.GetLocalControllerVelocity(controller) to world space. It works now when I rotate and throw an object but I now have to hold my hand/controller at a certain angle to throw the object forward. If I hold my hand at different angles I end up throwing the object up/down/left/right depending on that angle. I can make another video if needed.

    I've also looked up OVRInput.GetLocalControllerVelocity() on the Oculus site to see what options I have with that but there isn't much info there.

    This is the line of code I used to replace the original rigidbody.velocity assignment from my code.
    Code (CSharp):
    1. grabbedObject.GetComponent<Rigidbody>().velocity = transform.TransformDirection(OVRInput.GetLocalControllerVelocity(controller)) * speed;
     
  4. LedgeTheDev

    LedgeTheDev

    Joined:
    Jul 3, 2018
    Posts:
    1
    Was this ever figured out??
     
  5. Opp33

    Opp33

    Joined:
    Oct 30, 2017
    Posts:
    5
    Searching for a solution to the same issue, i found this discussion.
    Hoping the solution i found (in my case it works with minimum effort) can be useful for someone else, here you are my code:

    Code (CSharp):
    1.  
    2. //Drop here, in the inspector, the "TrackingSpace" element of your OVRPlayerController
    3. public Transform trackingSpace;
    4.  
    5. //.........REST OF THE CODE.........//
    6.  
    7. void Drop()
    8. {
    9.     Rigidbody objectRigidbody = grabbedObject.GetComponent<Rigidbody>();
    10.  
    11.     //Just multiply controller velocity by the direction your body is facing!!!
    12.     objectRigidbody.velocity = trackingSpace.rotation * OVRInput.GetLocalControllerVelocity(controller);
    13.     objectRigidbody.angularVelocity = OVRInput.GetLocalControllerAngularVelocity(controller);
    14. }
     
    Last edited: Mar 5, 2019
    Kyodan and AndersonSW like this.
  6. SteenPetersen

    SteenPetersen

    Joined:
    Mar 13, 2016
    Posts:
    23
    Alternatively, dont set the velocity of the object directly and instead use .AddForce.