Shell Eject - Position and Rotation

I am trying to do a shell eject when firing gun.

But how do i calculate the ‘Eject Port’ of the gun to line up the shell being ejected:

if (ejectedShell != null throwPosition1 != null)
{
Instantiate(ejectedShell.transform, throwPosition1.position, throwPosition1.rotation);
}

the ‘throwPosition1’ transform is the model of the gun (which is attached to hands and is a child of a ‘Weapons’ object which is a child of the FPS Controller’s Main Camera) that i assign in the Unity GUI

I Am Attaching my WeaponsActionScript.js for your reference

Note: You’ll see i do all my gun actions thru animations and animationEvents… WORKS GREAT. I just need to know how to calculate the position and rotation for the ‘Tip Of Barrel’ on gun for my muzzle flash emitter and the ‘Eject Port’ on gun for my Shell Ejection.

Please Help!

171412–6168–$weaponactionscript_256.js (10.9 KB)

You’re probably looking for transform.TransformPoint

But i do i tell where the tip of the barrel of my gun is (its Position and Rotation … and maybe apply so offset value to make it look like my emiter is shooting out a muzzle flash… If i can tell where my barrel tip is i should also be able to tell the offset to my eject port on the gun)

… I think… Or is there a better way to get the position and rotation of where you should be placing your muzzle flash emitter to get the cool gun shot effect?

You can either calculate it with offsets like you said, and then hard-code those values, or for better workflow and scalability you can create dummy game objects…
Both are parented to the gun.
One is located on the muzzle hole and is rotated so it’s Z axis is pointing away from the muzzle hole (towards the direction the gun is pointet at).
The second is located just where the eject hole is, and is rotated so that it’s Z axis is pointing away from the eject hole… (To the right of the gun, and a little to the top, so that cartriges will fly a little upwards).
When you instantiate your shells and muzzle flash - use the transform of each dummy game object (in world space… Use transform.position, and transform.rotation).

I am think i will also use the muzzle point as the source for the raycast for bullet hits (Im thinking that would be better than the transform.position of the gun itself… but rather this new gameobject i call muzzle that is directly at the tip of actual gun).

Sweet…

But how do i add some ‘force’ to it and maybe some ‘wobble’ so i looks like its being ‘Ejected’ for and with some wobble so they all DONT get ejected the same…

And do think i should use the particle system to render the muzzle flash or more like the sample and make a 3d model of a muzzle flash (put the ‘fire’ texture on it and do an instantiate with the muzzle holder thing as the source position and rotation) … then Destroy(muzzleFlash, 0.5)… Or Something like that…

I dont know if the more ‘Atmospheric’ Partical System would be better then the 3d model the machineGun Sample uses (Although the sample has the muzzleFlash object as apart of the same machineGun.fbx but mine would be its own muzzle.fbx with just the ‘flash’ cylnder that i instantiate and position like it was my shell eject but with no rigidbody so it stays there)

What do you Think?

BTW… All that stuff in the previous post was about the Shell Eject Method
you suggested (with the dummy objects) WORKED GREAT!

I was now asking hot to make shells have move a force and be a little more random as they spit out…

And then your thoughts on the best muzzle flash system.

Just in case my previous post was unclear

Maybe try giving the shell object a rigidbody. Attach a script that, upon instantiation, it selects a random rotation vector (constrained within a certain range to suit your taste) and give it an initial velocity ‘out’ of the ejection port (probably up the y-axis of the port object).

Can we give a quick little code snippet for th script to attach to the shell (which is already a rigidbody… Sorry for all the NOOB questions but this 3D stuff is all new to me… I am a C++ / C# Programmer by trade… I write Business apliications for hospitals during the day but i am trying to get into game develepment as a hobby)

try this to spin and fling the shells (run it only once! NOT in Update or FixedUpdate):

	rigidbody.AddRelativeForce (Random.Range(-1000, 1000), 
										  Random.Range(-1000, 1000), 
										  Random.Range(3500, 5000));
	
	rigidbody.AddRelativeTorque (Random.Range(-500, 500), 
											Random.Range(-500, 500), 
											Random.Range(-500, 500));

Untested in Unity, so fiddle with the numbers to get the results you want. The force is relative, so notice how your shell is instantiated (where it’s Z axis is pointing).
You don’t have to put the code on the object you instantiate BTW… You can run it from the script that instantiates your shells, since this code should only run once and not every frame

About the muzzle flash - particles will probably look better, but will be more demanding on your system. For performance it’s probably better to use a mesh with a texture on it. Animate the texture if you want…
Whatever you do - don’t instantiate it and destroy it every time you fire! NO NO NO! Bad programmer! :stuck_out_tongue:
Hide it (set renderer.enabled = false), Deactivate the game object of the muzzle flash (gameObject.active = false), fade the texture out (material.mainTexture.color.a = 0.0) - do whatever you think looks best, but avoid instantiating it and destroying it all the time. It’s expensive CPU wise and unnecessary…

Great info…

But about the not instantiating the muzzle flash for each gun shot for performance… If that is the case why is instantiate and destroy for each gun shot ok for the shell eject. Should i be doing that differrent as well?

In the case of the shell eject, theoretically you can have 200 shells flying at the same time if you’re firing a gatling gun. So you can’t have one object that you hide and show…
You could have maybe an array of 200 shells (let’s say that’s the maximum you could have at the same time) already created and hidden, which you move and show, then hide.
Managing those shells (which one is currently hidden, which one is shown, their positions and life span - when do you show one, and how long has it been shown and should you hide it now) will be a hastle and perhaps not worth the increase in performance. In some cases (like when you shoot one bullet per minute, or non at all) it will even be worse in performance, since you still need to manage the array - check if any bullets were shot recently or any shells are still visible… Unless you take an effort to write it as efficient as possible - which makes it even a bigger hastle :)).

But you have only one muzzle flash… And it is either shown or hidden. Managing it is simple and easy.