- Home /
SteamVR and Unity Physics; How to make it work?
Over the last few days I've been attempting to make a simple table tennis game using SteamVR and the HTC Vive. The idea seems simple enough; add a paddle gameobject to the controller, add colliders to the paddle and ball and it should all work itself out. But it doesn't, as the controllers are not Rigidbodies and their movement works separately from the Unity physics system. As such it creates issues like the paddle moving through the ball when the controller moves too quickly, or the velocity of the controller not mattering in the collision since its rigidbody's velocity is always zero.
One thing I've tried is to mark the object as IsKinematic and to match its position to the controller using MovePosition and MoveRotation on the paddle Rigidbody. I also turned on interpolation to make sure moving the paddle too quickly doesn't make it go through the ball. This sort of works, but with a huge delay; the paddle is always lagging way behind the controller - so this is not an option.
Another thing I've been trying is to implement my own physics code for the paddle, but in a way I feel like this shouldn't be necessary as the controller already tracks all of the same variables as the 'normal' rigidbodies do, such as velocity etc. I'm wondering whether there is a possibility for some kind of 'hybrid' collision, where I could plug the SteamVR controller's velocity, location etc. into the Unity Physics system.
Does anyone have any ideas on how I could approach this scenario?
@oppie85 - HI im currently having the same problem with my Vive game im trying to make. Ins$$anonymous$$d of using a picked up object eg. Ping pong paddle i want the controllers to act as the ping pong paddle but how will that work using Newton VR interactable item script? Especially as it requires the players to hold an item that uses this script. Im not to sure as this is my first ever Vive game and im not 100% familiar with unity.
How could i make it so that the Vive controllers model applys the correct physics to an inco$$anonymous$$g ball that can be hit away??
Please any assistance will be greatly appreciated
Answer by AntzyP · Oct 04, 2016 at 07:19 AM
As you've found, by making the paddle a child of controller object, its position is set by SteamVR and you don't get accurate collisions, and unity physics either doesn't work or works weirdly with it.
The simplest method I've found is to not make the paddle child of controller. Instead, keep the paddle separate as child of none, and join it with controller using a Fixed Joint with break force and torque kept to Infinity. This moves it using Unity physics engine and all physics interactions work perfectly!
Thanks a lot for this, it was a really good advice. I tried using the Fixed Joint and everything now works smoothly. I didn't know about this feature till now and it is pretty useful, specially with this VR cases.
Answer by oppie85 · Apr 22, 2016 at 08:40 AM
For anyone who has the same problem; this article provides a very good solution which is included in a free downloadable library:
http://www.vrinflux.com/newtonvr-a-free-physics-based-interaction-system-for-unity-and-the-htc-vive/
Answer by Spanky11 · Dec 08, 2016 at 08:44 AM
I had originally found a solution online that, instead of making the object a child of the controller, it kept it as a separate, non-kinematic object that tracked the position of the controller by dynamically changing the velocity so that it moved along with the controller. This worked, but the object was jerky, and lagged behind the controller. AntzyP suggests the better solution. Use a fixed joint. And you already have the code for this! Just check SteamVR_TestThrow.cs
I know this thread is old but I don't understand the advice. I look into the throw example but as far as I understand I need two rigid bodes to link together. But the S$$anonymous$$mVR controller has no rigid body. And if I add one, my grabbing action won't work any more. And if I ad one to the model, it won't get displayed any more. I am a bit puzzled here.
EDIT: Ok, I understand now. I need to add a rigid body to the controller's model and make it kinematic. Otherwise it will fall down. I could also disable gravity for the rigid body, but then the mass of the grabbed object will pull it down. $$anonymous$$inematic seems the way to go.
It has been awhile since I did this, but the answer by AntzyP using Fixed Joint worked perfectly. I would recommend checking it out.
Regarding the gravity, you do not work with gravity as it is controlled by the user and that would result in some weird behavior. If I remember correctly I tried using the "is$$anonymous$$inematic" option and did not work. Sadly I do not have a VR set anymore to double check.
Your answer
Follow this Question
Related Questions
Object movement choppy when picked up in VR 1 Answer
Can't move object in VR if it has children 0 Answers
[SteamVR] Drum physics 3 Answers
Revolver cylinder physics 0 Answers