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")
	{
		Debug.Log("killed");

		Destroy (gameObject);
	}
}

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


var cameraSwitcher : Transform;
//bullet
var bulletPrefab: Transform;

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

if(Input.GetButton("Shoottrigger"))
	{
		if(Input.GetButtonDown("Shoottrigger2"))
			{
			var bullit = Instantiate(bulletPrefab, 
											cameraSwitcher.transform.Find("spawnPoint").transform.position, 
											Quaternion.identity);
			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

Thanks

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.