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.
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.
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.
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:
//Drop here, in the inspector, the "TrackingSpace" element of your OVRPlayerController
public Transform trackingSpace;
//.........REST OF THE CODE.........//
void Drop()
{
Rigidbody objectRigidbody = grabbedObject.GetComponent<Rigidbody>();
//Just multiply controller velocity by the direction your body is facing!!!
objectRigidbody.velocity = trackingSpace.rotation * OVRInput.GetLocalControllerVelocity(controller);
objectRigidbody.angularVelocity = OVRInput.GetLocalControllerAngularVelocity(controller);
}
I can’t believe how easy it was. I was wondering why throwing stopped working. Apparently, you don’t need to multiply by rotation using “Stage” tracking, but in floor mode you need to! I am so thankful.