Bullet being destroyed when hitting terrain

I am trying to figure out a method to make my bullet destroy when it collides with the terrain. Right now, it will hit the terrain, and continue sliding across the ground until it’s life expectancy, and is then destroyed. Huge noob here, sorry for lack of further information. Any help would be greatly appreciated.

bullet.js


public var bulletPrefab : Transform;
public var bulletSpeed : float = 6000;

function Update ()
{
if (Input.GetKey(KeyCode.Mouse0))
{

if (!bulletPrefab || !bulletSpeed)
{
Debug.Log(“[Shoot] ‘bulletPrefab’ or ‘bulletSpeed’ is undefined”);
}

else
{
var bulletCreate = Instantiate(bulletPrefab, GameObject.Find(“SpawnPoint”).transform.position, Quaternion.identity);
bulletCreate.rigidbody.AddForce(transform.forward * bulletSpeed);
}
}
}


This seems to work fine, the bullet is destroyed after a preset time, but the collision for them to be destroyed when they hit the terrain is where I am stuck. Much thanks in advance for any suggestions.

Your bullet’s time out has nothing to do with this script. What you need to do is inside the bullet’s script (or wherever you have it’s time out function), is add a collision detection. Adding something (but maybe not exactly) like this would do it;

function OnCollisionEnter()
      yield;
      Destroy(gameObject);
}

You can read about the OnCollisionEnter here;
http://unity3d.com/support/documentation/ScriptReference/Collider.OnCollisionEnter.html

P.s. Oh and when posting code please use the CODE tags it make’s it a lot easier for others to read.

My apologies, I will be sure to do so next time, and thank you for the prompt response. I will let you know how I make out.