- Home /
Bullet won't move with gun?
I've just implemented gun movement into my slowmotion bullet dodging game so that the player will be fired at from different places rather than all in the one limb. Unfortunately, the bullet that sits in the barrel of the gun won't move with the gun, even if i attach the exact same movement script to it as is on the gun itself, it'll then fly off out of the arena never to be seen again.
I'm wondering how could I constrain the bullet to the guns position without setting it as a child of the gun, as when I use the child feature the bullet seems to rotate randomly and hover above the gun as if it's speed is double the guns speed.
Is there any simple (or even complicated?) way to lock the bullets position to the gun?
Because it hides it. And also because I messed up somewhere and made it so the bullet instantiates on the bullet.
If you don't want the player to see the bullet, don't even instantiate it until you need it.
But I can't seem to instantiate it in the right spot otherwise...
Answer by oasisunknown · Jun 05, 2014 at 11:20 PM
I don't understand why you cant use an empty game object as your spawnPoint for the bullets. Then when you Instantiate the bullet just feed in the spawnPoints transform.
The empty game object used as the spawnPoint would not have any scripts attched and would only be parented to the gun as a transform reference so you should not have any of the excess speed movement issues you were talking about that you had for the bullets.
We've got a winner. I don't know why I did this for everything else and not the bullets...
This is exactly what i've done, and through debugging the spawnPoint is where it is supposed to be - but bullets are spawning at that point. Also after debugging i found that if the spawnpoint is NOT set as a child then the spawn location is correct.
void ShootBullet(RaycastHit hit)
{
GameObject bulletProjectileInstance = Instantiate(bulletProjectile, bulletProjectileStartPosition.transform.position, Quaternion.identity) as GameObject;
Destroy(bulletProjectileInstance, 2f);
}
Answer by gooncorp · Jun 02, 2014 at 10:41 PM
one way to solve it is to use a rigidbody and let the physics do the work for you. instantiate the bullet, addrelative force to it using guns trajectory etc. make sure you delete it quickly as this way can be a bit heavier.
I'm still confused. I have a rigid body on the bullet already.