Collision problem

var rocket : Rigidbody;
var speed = 10.0;

function FireRocket()
	{
	var rocketClone : Rigidbody = Instantiate(rocket,transform.position,transform.rotation);
	rocketClone.velocity = transform.forward * speed;
	}
function OnCollisionEnter ()
	{
	Destroy (gameObject);
	}
	
function Update (){
	if (Input.GetButtonDown ("Fire1")){
		FireRocket();
		}
}

Hi there! I’m new to unity and I’m just learning how to make basic game elements, right now I’m trying to have this rocket disappear when it hits a wall or some object, but it does not do that. I wasn’t able to find many other relevant questions like mine.

The object that shoots out is a cube (I know, not a missile) and at first it collides with terrain - that didn’t work. So I had the cube collide with a cube and that didn’t work either. The colliders are all on and I feel like I am missing something fairly obvious.

Thanks in advance for the help!

The OnCollisionEnter code is in the weapon’s script, but it should be in the missile script - that’s the missile that collides with things, not the weapon!

  • Weapon script:
var rocket : Rigidbody;
var speed = 10.0;

function FireRocket(){
	var rocketClone : Rigidbody = Instantiate(rocket,transform.position,transform.rotation);
	rocketClone.velocity = transform.forward * speed;
}

function Update (){
	if (Input.GetButtonDown ("Fire1")){
		FireRocket();
	}
}
  • Rocket script:
function OnCollisionEnter (){
	Destroy (gameObject);
}