physics – Firearm magazine ejection

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:

enter image description here

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:

enter image description here

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