Gun shooting problems

I’m trying to make a fps game. I’ve assigned my gun script to my gun, and put my gun under my fps camera controller. I’ve also made my prefab for the bullet and assigned it. But when I shoot it, the bullet appears, and then it seems to hit the gun and then go straight down. I’m pretty sure I programmed the bullet to ignore the gun. Anyway, here’s the code:

//Attach to empty object, in front of barrel
var projectile : Rigidbody;
var speed = 20;
var fireRate = .5;
var mySound : AudioClip;

function Update()

function Fire()
	if( Input.GetButtonDown( "Fire1" ) ) {
		var instantiatedProjectile : Rigidbody = Instantiate(
projectile, transform.position, transform.rotation );
		instantiatedProjectile.velocity =
transform.TransformDirection( Vector3( 0, 0, speed ) );
		Physics.IgnoreCollision( instantiatedProjectile. collider,
transform.root.collider );

You don’t have to program the bullet to ignore the gun. Create an empty game object, place it right in front of the gun and make it a child of the gun. Call it firePoint. And have the bullet instantiate and fly ahead from there.

There is, however, a better alternative. Don’t use physics for the shooting. Use Raycasts. I’m sure you’ll find the info you need if you search for Physics.Raycast on the Unity Script Reference page.

Basically, what you do is you cast an invisible ray (hence the name “Raycast”) from a point to a direction with a maximum range limit you define, lets say if it hits a collider whose gameObject is tagged “enemy” then you make it send a message to the gameObject, invoking a function that lowers the health and eventually kills the “enemy”.

Hope I’ve been informative. Have a nice day.

if the gun is meant to use bullets, you want raycast, if it is projectiles (slingshot, rocket launcher, ect) use prefabs :slight_smile: If you use a prefab, you can prevent the projectile from appearing until you press the button, also make sure you have gravity off in the rigidbody if you don’t want gravity to take its toll immediately