Firearm magazine ejection

I am playing around with a VR game where the player can fire a hand gun. When the magazine is empty they can press a button on the controller to eject the magazine from the gun. As its VR, I would like to have the player be able to pick up the magazine again, if they need to.

Currently when the eject button is pressed, the rigidbody of the magazine is enabled and it falls from the gun.

My question : The player can hold the grip side pointing up and the magazine will drop through the gun or hold the gun sideways and the same will happen.

If the player hold the gun in any of these orientations, the magazine needs be released but stay in the grip and only slide out as the gun is turned so that gravity pulls it.

My first instinct was to use code to animate the magazine and only enable the rigidbody when it leaves the gun. Another way to use 5 box colliders to emulate the walls of the grip and that way contain the movement.

I was just wondering if there was a better way to do this.

• It sounds like you might want a prismatic joint, allowing the magazine to slide only along its exit path, and which you can destroy/disable to drop the magazine once it's slid out past the end of its slot. Previous Q&A on configuring a sliding door may be useful here, especially if you're working in Unity (your tags don't say). Sep 5 at 17:50
• @DMGregory, sorry, yes it is Unity. I would not have thought about searching for sliding door mechanics . Brilliant ! I will post my findings here. Sep 5 at 18:00
• @DMGregory I was about to answer and include the very Q&A you linked, I was too slow :P Sep 5 at 18:26
• DMGregory pointed me to the right place. Thank you. I found this video very helpful to setup the 3D slider joint for Unity Sep 5 at 19:08

A Configurable Joint seems what you're looking for, as pointed out by DMGregory in this previous question.

I tried to implement a sliding magazine in a gun using the following setup:

• The Weapon GameObject is an entity with a RigidBody component (needed for configuring the Joint)
• The Magazine GameObject is a different GameObject and has a RigidBody as well

I made the following assumptions to make this system work:

• Weapon's RigidBody has its property isKinematic set to True when being held by players - you attach it to their hand for interaction purposes. If dropped or thrown, isKinematic is set to False so that gravity can do the work
• Magazine behaves as follow:
• When inserted: it is parented to Weapon and its isKinematic property is set to True. This way it will be integral with the weapon and move/rotate accordingly
• When reloading: its parent is removed and its isKinematic is set to False, actually behaving as a generic physics-driven GameObject. However, its physics behaviour is influenced by a Configurable Joint that only allows sliding movement along a single direction

I managed to configure the Joint as follows:

Actual values depend on your actual Magazine dimensions. The main idea is to enable movement along the ejection direction only. By assigning a Connected Body, you can work in local coordinates.

I defined the Connected Anchor (the centre of the "track" the magazine moves along before being ejected) at a position such that, given an appropriate Linear Limit distance (in my case it's half the magazine's height), one end is the starting position of the magazine (same position as when inserted) and the other end is when the magazines starts falling:

Via script, when the magazine reached the bottom end, you can disable/remove the Joint to let the magazine free.

• My magazine has UP as the Y axis. When I set the Axis in the Joint to Y....the magazine just moves to the Anchor. I have it like you show in your example with X as the axis and the secondary as Y. I have the Y Motion on limited and it works. Thank you so very much to you. Sep 5 at 19:14