Destroy object on bullet collision

I can’t seem to get my code working. When a bullet hits and enemy, it is supposed to destroy it. When I run the game now, it occasionally works, but very rarely. The Debug.Log(“killed”) normally is not printed in the log.

The enemy is a capsule with a rigidbody, a capsule collider, and the following code.

function OnCollisionEnter( hit : Collision)

	Debug.Log("collision working");
	if(hit.gameObject.tag == "shooter")

		Destroy (gameObject);

the script that shoots the bullet is the following and is applied on an empty.

var cameraSwitcher : Transform;
var bulletPrefab: Transform;

function Update () 
cameraSwitcher.transform.Rotate(Input.GetAxis ("Vertical") * -rotateSpeed, Input.GetAxis ("Horizontal") * rotateSpeed, rotationLock);

			var bullit = Instantiate(bulletPrefab, 
			bullit.tag = "shooter";
			bullit.rigidbody.AddForce(cameraSwitcher.transform.forward * 40000);

The bulletPrefab is simply a prefab of a sphere with a sphere collider and rigidbody attached.

Any help is appreciated


Everyone wants to use real bullets! Consider using a raycast instead, unless your bullets move so slow they can be seen (Matrix style). The problem is likely your bullet passes through the body too fast. In one frame it’s one one side, by the next frame it’s on the other side already.

You could make the bounding box for your bullet ‘longer’ in the direction of travel so it has a better chance of colliding with the body. But really, look into raycasting if you can.