- Home /
How can I make an object invulnerable to collision with a specific object?
I am making a StarFox like game. In the game meteors are hurtling towards the characters ship, if the ship collides with a meteor the ships current health decrease. However, when I shoot the ships gun the bullets also collide and hurt the ship causing it to destroy itself. How can I make the ship invulnerable to the bullets but still take damage from the meteors?
Here is my code if it helps at all...this method is in a script for the ships attributes which declares the ships health and the amount of damage the ship takes from a collision.
void OnCollisionEnter()
{
// Decreases our current health when hit by an object
currentHealth = currentHealth - standardHit; //standardHit declares damage taken by collision
// Tests if current health is less than 25
if(currentHealth < 25)
{
// Writes to our debug log
Debug.Log ("Your health is low!");
}
// Tests if current health is less than or equal to 0
if(currentHealth <= 0)
{
// Destroys our game object
Destroy(gameObject);
}
}
Answer by dazzsnow · Oct 25, 2013 at 05:53 AM
go to Edit/Project Settings/Physics you will then see Layer Collision Matrix in inspector panel. Give ship a different layer to missile / bullet. untick relevant box's in the matrix.
Just in case, first step is to make 2 new layers (player, playerBullet?) in the layer menu. Names and numbers don't matter. I think 8 is the first empty layer slot.
Wow that was a lot simpler than I thought it'd be haha. But it worked! Thank you!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Minor bugs in my gun shooting script 1 Answer
Destorying prefab and instanstiating again? 0 Answers
Distribute terrain in zones 3 Answers