Bullet explosion problem

Im currently working on a topdown shooter where the player is a sort of tank. I can move around and shoot and now im adding some particle effects. The effects do work but im having a problem.
When the player clicks the left mouse button it wil shoot a bullet. The bullet leaves some fire ( particle effect ), but the problem comes when i wanted to have an explosion when the bullet collides with something.
Actually this works fine to…
but it also shows the explosion in front of the tank when shooting


As you can see it also shows an explosion in front of the tank.
Here is also my code that is attached to the bullet

// the explosion particle effect
var explosion : GameObject;
// the tank
var speler : Transform;


function OnCollisionEnter( collision : Collision )
{
	var contact : ContactPoint = collision.contacts[0];
	var rotation = Quaternion.FromToRotation( Vector3.up, contact.normal );
	var explosion : GameObject = Instantiate(
	explosion, contact.point, rotation );
	Destroy( gameObject );
}
function Update ()
{
// ignore the collision with the tank so there shouldn't be a explosion in front of the tank :(
Physics.IgnoreCollision(transform.collider , speler.collider);
}

Nobody knows a solution ? :frowning:

Please help me…

Try setting Object tags :slight_smile:

function OnCollisionEnter (collision : Collision) {
	//print(collision.collider.tag);
	if(collision.collider.tag != "Bullets"  collision.collider.tag != "Tank" ){
	var contact : ContactPoint = collision.contacts[0]; 
	var rotation = Quaternion.FromToRotation(Vector3.up, contact.normal); 
	if(collision.collider.tag == "Enemy"){
	var instantiatedExplosion : GameObject = Instantiate (explosion, contact.point, rotation); 
	}

thank you :smile:

im gonna try that :wink:

problem still exists :frowning:

im getting the idea the problem is not with collision but with the particle effect itself ?
can i maybe turn off that it automatiacly emits the particles on spawn ?

Looks like nobody knows an answer :frowning:

Please help me :lol:

function OnCollisionEnter( collision : Collision )
{
   Debug.Log(collision.contacts.Length);
   var contact : ContactPoint = collision.contacts[0];
   var rotation = Quaternion.FromToRotation( Vector3.up, contact.normal );
   Instantiate(explosion,contact.point,rotation);
   Destroy( gameObject );
}

Let me know what it prints for the length.

Ok :wink:

i’m gonna add that code and will let you know soon what it says about the length

Ok this is the code i have now

var explosion : GameObject;
   
function OnCollisionEnter( collision : Collision ) 
{ 
   Debug.Log(collision.contacts.Length); 
   var contact : ContactPoint = collision.contacts[0]; 
   var rotation = Quaternion.FromToRotation( Vector3.up, contact.normal ); 
   Instantiate(explosion,contact.point,rotation); 
   Destroy( gameObject ); 
}

and this is what my debug log says:

1
UnityEngine.Debug:Log(Object)

Sorry… i did forget to post the errors to:

NullReferenceException: The prefab you want to instantiate is null,
UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation)

this error is shown 5 times ^^

I think this was in the FPS tutorial on how to prevent the rocket launcher’s rocket from blowing up itself when shooting.

// Ignore collisions between the missile and the character controller

Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);

Perhaps that might work.

I’v tried this already and that wasn’t the problem :wink:

It’s only colliding with one object, so it’s likely a particle issue, and not a collision issue. Perhaps you are accidentally placing the explosion particle before you move it to the desired location…

i dont understand what you mean :frowning:

please explain a bit more :smile:

make an gameobject and name it gun_spawn pit it in the barle of the tank and then move it away from you so the rocket is not exploding on you.

im makeing a tank game to but my prob is the rocket dose not hirt the ai tanks and thay dont hirt me.