Projectile wait for second collision? (solved)

Hi, I’m making a simple shooter game in unity and i have set up the bullet prefab that is fired by all players like so;

var rot : Quaternion;

function Update () {
}

function OnCollisionEnter (collision : Collision) {

	var contact : ContactPoint = collision.contacts[0];

	yield WaitForSeconds(0.05);
	Destroy(gameObject);  
}

function OnTriggerEnter(col : Collider) {
col.SendMessageUpwards("ApplyDamage", transform.rotation, SendMessageOptions.DontRequireReceiver);
}

function Start()
{
yield WaitForSeconds(0.7);
Destroy(gameObject);  
}

the problem i am having is that when a player spawns/fires a bullet it is already colliding with their player controller, so the bullet will apply damage to whoever fired it.

How can i make by bullet not damage the first thing it hits, but instead damage the second thing?

never mind, i just solved it;

var rot : Quaternion;
var isFirstHit : boolean = true;
var bulletLive : boolean = true;
var firstCollider : Collider;

function Update () {
}

function OnCollisionEnter () {

	//var contact : ContactPoint = collision.contacts[0];


	if(!bulletLive){
	yield WaitForSeconds(0.05);
	Destroy(gameObject);  
	}
	
	
}

function OnTriggerEnter(col : Collider) {
	
	if(isFirstHit){
	firstCollider = col;
	isFirstHit = false;
	}
	
	if(col != firstCollider){
	col.SendMessageUpwards("ApplyDamage", transform.rotation, SendMessageOptions.DontRequireReceiver);
	bulletLive = false;
	}
	
}

function Start()
{
yield WaitForSeconds(0.7);
Destroy(gameObject);  
}

instead of making a hacky implementation by skipping the player collision, you can just tell the physics system to ignore the collision in the first place.

1 Like