I have started making a PC game using Unity. It's just a test project, where I'm making as basic as possible FPS shooter.

I'm using the FPS Controller prefab, and I have also a rifle model attached to it. The result is pretty much what I wanted.

Now I'm considering how I want to create projectiles for the gunfire. I think it will be best for me to create each bullet as just being a ray from the gun barrel heading forwards, OR I might create rigidbodies from the gun barrel I'm not sure yet.

My question is: What is the proper way to reference the point in space in which the gun barrel current ends? At the moment, I am thinking just attach ANOTHER gameObject to the gun model, and just manually position it where the barrel finishes. But this seems 'cheaty' and I think perhaps there is a better way (something like mesh.position.z + mesh.length, but I can't seem to find anything like mesh.length).

I haven't posted code at all, because I haven't really written any on this project yet.

I would also be very interested to hear the pro-tip version of how best to simulate this gunfire (ie. is it best to use rigidbodies? or somehow use raycast or something? or some better way that I haven't even dreamt of)

  • 1
    \$\begingroup\$ Your PPS is a different question. Ask it in its own post if you still have questions after searching for related material. \$\endgroup\$ – DMGregory Sep 1 '17 at 16:49

Your proposed method of creating an empty game object to "mark" the muzzle of the weapon is generally considered a good approach.

This gives your model artists direct visual control over where they want the projectile / muzzle effects to spawn, without dealing with the complexities of some kind of automated mesh analysis. They can even set up this marker object in their 3D tools so it's imported as part of the same asset file.

This also gives you flexibility to go wild with the weapon shapes without breaking the spawn position. You can have weapons with lights, bayonettes, or launchers that protrude past the barrel, or with multiple muzzles in exotic arrangements, just by positioning (optionally several) marker objects.

The overhead of one extra transform is generally negligible, and well worth it for the simplicity and flexibility it offers.

  • 1
    \$\begingroup\$ Or even multiple barrels, just mark each one and loop over all of them. \$\endgroup\$ – Draco18s no longer trusts SE Sep 1 '17 at 16:47
  • \$\begingroup\$ awesome, im so glad to hear these answers :) thanks again. i will re-ask the projectile question after i attempt a little more reading on the subject \$\endgroup\$ – Big T Larrity Sep 1 '17 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.