- Home /
How do I give my ejected bullet shell a speed plus the player's current speed?
If my player is running around in the game and I eject a shell from his gun when I fire, how do I make sure the speed the shell travels is in addition to the speed the player is already moving?
Basically, I want it to always look like the shell is flying out and off to the right of the gun regardless of what direction the player is running around in or how fast they are moving, as it looks wrong just now with the shell looking like it's flying more to the left if I move right and more to the right if I move left, so I want to set the shell's speed on top of whatever speed the player is already moving in whatever direction.
You know, like if I throw something while stationary vs in a vehicle moving at 60mph, the object will travel at its normal speed plus my current speed so it will actually be moving faster if I throw it from the moving car.
You can use the reference of the rigibody of the parent. It will give you the current velocity of the player and then you can add it to the shell.
Shell's and bullets appear to be slightly confused in your post, though I'm sure not in your $$anonymous$$d. That is to say I think you're asking about two separate object behaviors.
For a shell ejection a lot depends on motion change and the relative measurement you're observing. If the weapon is moving at a constant rate throughout the duration of the ejection, the relative trajectory of the shell should be consistent relative to the weapon, but will not remain consistent relative to the world (which can be difficult to correctly 'see' at speed). When the weapon changes direction or speed during or just after ejection, there won't be a consistent trajectory relative to the weapon.
If the shell and/or bullet are child objects of the weapon, they're moving with the weapon. If they are 'unparented' at ejection (where firing force was added to the child before unparenting), the local system should do what you're asking about until it is uncoupled. That is a rather common solution.
No, it's all just the shell casings ejecting from the gun that I'm on about. $$anonymous$$y bad if I've not been properly clear. The shell casings aren't children of the gun; they're just their own prefab that I instantiate and "eject" (via applying force) out the side of the gun each time the player fires.
Answer by JVene · Aug 21, 2018 at 08:37 PM
Get the Rigidbody component of the gun. Instantiate the casing, let's say it's 'c'. Get 'c's Rigidbody, then copy the gun's velocity with something like:
c_rb.velocity = gun_rb.velocity;
Where c_rb is the casing's Rigidbody, and gun_rb is the gun's Rigidbody. Then apply the ejection force.
Well, I tried the following and it didn't work (the part at the bottom with "shellsRigidbody.velocity = gunsRigidbody.velocity;" says there's an error with both of those parts):
This is what I have inside my Update function:
GameObject gun = GameObject.Find("GunNewPC");
if (gun != null)
{
Rigidbody gunsRigidbody;
gunsRigidbody = GetComponent<Rigidbody>();
}
GameObject shell = GameObject.Find("BulletShell");
if (shell != null)
{
Rigidbody shellsRigidbody;
shellsRigidbody = GetComponent<Rigidbody>();
}
Instantiate(bulletShell, transform.position, transform.rotation); // This is set to just off to the side of the gun
shellsRigidbody.velocity = gunsRigidbody.velocity;
What did I do wrong?
Note: The object this script is attached to is a child of my gun object, which is in turn a child of my player object, just in case that's relevant.
This is a simple little issue. The gunsRigidbody and the shellsRigidbody are created inside the brackets of an 'if' statement, which means they completely evaporate outside the brackets of those 'if' statements. The are considered local (and only local) to the block of code between those brackets. The technical phrase is that they only have scope inside the brackets.
$$anonymous$$ove the declaration of Rigidbody gunsRigidbody and Rigidbody shellsRigidbody outside (and before you use them) the brackets of the if statments. This does not apply to the call to GetComponent, those are not affected or part of the problem.
Note, this means that one or the other could be null, so consider testing.
Note, it's not working. Here's what I have so far:
void Update()
{
if (OVRInput.GetDown(OVRInput.Button.SecondaryIndexTrigger) || Input.Get$$anonymous$$ouseButtonDown(0)) // Secondary means Right Hand for Oculus Touch
{
audioSource.Play();
RaycastGun();
Rigidbody gunsRigidbody;
Rigidbody shellsRigidbody;
Instantiate(bulletShell, transform.position, transform.rotation); // This is set to just off to the side of the gun
GameObject gun = GameObject.Find("GunNewVR");
gunsRigidbody = gun.GetComponent<Rigidbody>();
GameObject shell = GameObject.Find("BulletShell");
shellsRigidbody = shell.GetComponent<Rigidbody>();
shellsRigidbody.velocity = gunsRigidbody.velocity;
Instantiate(gunFlash, shotOrigin.position, transform.rotation); // The barrel of the gun
}
}
Clearly I'm not understanding something here (and this is my trying to interpret what you're saying and put it in there correctly)--maybe there's something you think is obvious but isn't to me--so if you could just show me how it's supposed to be written properly, it really would be appreciated.
I'm not learning a single thing from all my mistakes; I'm just wasting time making a lot of mistakes.