How can I not clip through the walls in VR?

I’m making a vr game right now and I’m able to stick my face in the wall no problem, in other games I’ve seen however, it blocks your face from going clipping through objects. How can I prevent the camera or player from pushing through the wall or ground?

Since the VR camera is locked to the HMD by default, you have basically two options:

  1. Parent a sphere mesh to the VR camera so that it has the approximate dimension of a head. Make sure it can produce overlap events with the rest of the world. Initially the sphere is transparent, but when it overlaps (On Begin Overlap) with the walls you apply a semi opaque black material to it. On End Overlap you turn it back to transparent. This is an easy way to tell the player you shouldn’t stick your head through a wall, but still allow them to freely move in VR.
  2. You use a sphere like above but you don’t parent it to the VR camera, you simply keep it in sync with the VR Camera by assigning it the same position of it On Tick, taken directly from the low level HMD position function. Once the VR camera, and hence the sphere overlaps with a wall, you unlock the VR Camera from the HMD so you let the player bend into the well but its view remains where the camera has stopped. At this point you keep moving the sphere along the real HMD location and once it stops overlapping with the well you re-lock the camera to the HMD.

You can note that option 2 may potentially create a discomfort to the player since when the camera is re-locked to the HMD the HMD may be in a different position as when it was unlocked. The result is that the view of the player “jumps” from one location/rotation to another, which may cause problems to a sensitive person.I would be careful with adopting it.

You may also want to study how the VR Expansion plugin implements this functionality. Maybe it is already what you are looking for.

2 Likes