- Home /
VR pushable button,
I'm wanting to add a button that the user can push and have it spring back when no longer pushed by the user.
I've followed various tutorials, but none have produced the desired results. Ideally I'd like to achieve this without any prefabs as I believe it should be quite easy to achieve.
Thanks, Dave,
Answer by TheKnightsofUnity · Aug 18, 2019 at 04:35 PM
Hello @BottlecapDave !
You can use a Spring Joint component to solve your problem.
To achieve that, attach a Spring Joint component to the moving part of the button. This will automatically create a Rigidbody, if there had not been one beforehand.
Next, freeze the rigidbody's position in the X and Y axis (assuming that your button should move along the Z axis - if not, freeze other axis accordingly), and the rotation on all axes.
Then, set up the rigidbody on the wall or any other object you want the button to be attached to. If you don't want the object to respond to physic forces, you should check the checkbox next to 'Is Kinematic'.
After that, plug the 'wall' rigidbody into Spring Joint's 'Connected Body' field, and configure the anchor. Make sure that the 'Auto Configure Connected Anchor' box is checked, so that you don't have to set it up manually. Also, don't forget to check 'Enable Collision' so the player can't push the button too far in.
With all that set up, what's left is to tweak the Spring and Damper parameters. A set of values that feels like a believable button is 100 for the spring, and 1 for the damper.
Keep in mind that this approach is based on physics, and as such it will suffer from its drawbacks - like the fact that the player will be able to 'pull' the button out if they can reach under it. This can be limited via a little bit of scripting, though.